forked from ServiceStackApps/RestFiles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFilesService.cs
157 lines (128 loc) · 5.49 KB
/
FilesService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.IO;
using System.Linq;
using System.Net;
using RestFiles.ServiceModel;
using RestFiles.ServiceModel.Types;
using ServiceStack;
using ServiceStack.IO;
namespace RestFiles.ServiceInterface
{
/// <summary>
/// Define your ServiceStack web service request (i.e. Request DTO).
/// </summary>
public class FilesService : Service
{
public AppConfig Config { get; set; }
public object Get(Files request)
{
var targetPath = GetAndValidateExistingPath(request);
var isDirectory = VirtualFiles.IsDirectory(targetPath);
if (!isDirectory && request.ForDownload)
return new HttpResult(VirtualFiles.GetFile(targetPath), asAttachment: true);
var response = isDirectory
? new FilesResponse { Directory = GetFolderResult(targetPath) }
: new FilesResponse { File = GetFileResult(targetPath) };
return response;
}
public object Post(Files request)
{
var targetDir = GetPath(request);
if (VirtualFiles.IsFile(targetDir))
throw new NotSupportedException(
"POST only supports uploading new files. Use PUT to replace contents of an existing file");
if (!VirtualFiles.DirectoryExists(targetDir))
{
VirtualFiles.WriteFile(targetDir.CombineWith(".temp"), "");
VirtualFiles.DeleteFile(targetDir.CombineWith(".temp"));
}
foreach (var uploadedFile in base.Request.Files)
{
var newFilePath = targetDir.CombineWith(uploadedFile.FileName);
VirtualFiles.WriteFile(newFilePath, uploadedFile.InputStream);
}
return new FilesResponse();
}
public void Put(Files request)
{
var targetFile = VirtualFiles.GetFile(GetAndValidateExistingPath(request));
if (!Config.TextFileExtensions.Contains(targetFile.Extension))
throw new NotSupportedException("PUT Can only update text files, not: " + targetFile.Extension);
if (request.TextContents == null)
throw new ArgumentNullException("TextContents");
VirtualFiles.WriteFile(targetFile.VirtualPath, request.TextContents);
}
public void Delete(Files request)
{
var targetFile = GetAndValidateExistingPath(request);
VirtualFiles.DeleteFile(targetFile);
}
private FolderResult GetFolderResult(string targetPath)
{
var result = new FolderResult();
var dir = VirtualFiles.GetDirectory(targetPath);
var subFolders = dir.Directories.ToList();
foreach (var subDir in subFolders)
{
if (Config.ExcludeDirectories.Contains(subDir.Name)) continue;
result.Folders.Add(new Folder
{
Name = subDir.Name,
ModifiedDate = subDir.LastModified,
FileCount = subDir.GetFiles().Count(),
});
}
foreach (var fileInfo in dir.GetFiles())
{
result.Files.Add(new ServiceModel.Types.File
{
Name = fileInfo.Name,
Extension = fileInfo.Extension,
FileSizeBytes = fileInfo.Length,
ModifiedDate = fileInfo.LastModified,
IsTextFile = Config.TextFileExtensions.Contains(fileInfo.Extension),
});
}
return result;
}
private string GetPath(Files request)
{
return Config.RootDirectory.CombineWith(GetSafePath(request.Path));
}
private string GetAndValidateExistingPath(Files request)
{
var targetPath = GetPath(request);
if (!VirtualFiles.IsFile(targetPath) && !VirtualFiles.IsDirectory(targetPath))
throw new HttpError(HttpStatusCode.NotFound, new FileNotFoundException("Could not find: " + request.Path));
return targetPath;
}
private FileResult GetFileResult(string filePath)
{
var file = VirtualFiles.GetFile(filePath);
var isTextFile = Config.TextFileExtensions.Contains(file.Extension);
return new FileResult
{
Name = file.Name,
Extension = file.Extension,
FileSizeBytes = file.Length,
IsTextFile = isTextFile,
Contents = isTextFile ? VirtualFiles.GetFile(file.VirtualPath).ReadAllText() : null,
ModifiedDate = file.LastModified,
};
}
public static string GetSafePath(string filePath)
{
if (string.IsNullOrEmpty(filePath)) return string.Empty;
//Strip invalid chars
foreach (var invalidChar in Path.GetInvalidPathChars())
{
filePath = filePath.Replace(invalidChar.ToString(), string.Empty);
}
return filePath
.TrimStart('.', '/', '\\') //Remove illegal chars at the start
.Replace('\\', '/') //Switch all to use the same seperator
.Replace("../", string.Empty) //Remove access to top-level directories anywhere else
.Replace('/', Path.DirectorySeparatorChar); //Switch all to use the OS seperator
}
}
}