-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathFilesService.cs
More file actions
142 lines (114 loc) · 4.82 KB
/
FilesService.cs
File metadata and controls
142 lines (114 loc) · 4.82 KB
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
using System;
using System.IO;
using System.Net;
using RestFiles.ServiceInterface.Support;
using RestFiles.ServiceModel;
using RestFiles.ServiceModel.Types;
using ServiceStack;
using File = System.IO.File;
namespace RestFiles.ServiceInterface
{
/// <summary>
/// Define your ServiceStack web service request (i.e. Request DTO).
/// </summary>
public class FilesService : Service
{
/// <summary>
/// Gets or sets the AppConfig. The built-in IoC used with ServiceStack autowires this property.
/// </summary>
public AppConfig Config { get; set; }
public object Get(Files request)
{
var targetFile = GetAndValidateExistingPath(request);
var isDirectory = Directory.Exists(targetFile.FullName);
if (!isDirectory && request.ForDownload)
return new HttpResult(targetFile, asAttachment: true);
var response = isDirectory
? new FilesResponse { Directory = GetFolderResult(targetFile.FullName) }
: new FilesResponse { File = GetFileResult(targetFile) };
return response;
}
public void Post(Files request)
{
var targetDir = GetPath(request);
var isExistingFile = targetDir.Exists
&& (targetDir.Attributes & FileAttributes.Directory) != FileAttributes.Directory;
if (isExistingFile)
throw new NotSupportedException(
"POST only supports uploading new files. Use PUT to replace contents of an existing file");
if (!Directory.Exists(targetDir.FullName))
Directory.CreateDirectory(targetDir.FullName);
foreach (var uploadedFile in base.Request.Files)
{
var newFilePath = Path.Combine(targetDir.FullName, uploadedFile.FileName);
uploadedFile.SaveTo(newFilePath);
}
}
public void Put(Files request)
{
var targetFile = GetAndValidateExistingPath(request);
if (!this.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");
File.WriteAllText(targetFile.FullName, request.TextContents);
}
public void Delete(Files request)
{
var targetFile = GetAndValidateExistingPath(request);
File.Delete(targetFile.FullName);
}
private FolderResult GetFolderResult(string targetPath)
{
var result = new FolderResult();
foreach (var dirPath in Directory.GetDirectories(targetPath))
{
var dirInfo = new DirectoryInfo(dirPath);
if (this.Config.ExcludeDirectories.Contains(dirInfo.Name)) continue;
result.Folders.Add(new Folder
{
Name = dirInfo.Name,
ModifiedDate = dirInfo.LastWriteTimeUtc,
FileCount = dirInfo.GetFiles().Length
});
}
foreach (var filePath in Directory.GetFiles(targetPath))
{
var fileInfo = new FileInfo(filePath);
result.Files.Add(new ServiceModel.Types.File
{
Name = fileInfo.Name,
Extension = fileInfo.Extension,
FileSizeBytes = fileInfo.Length,
ModifiedDate = fileInfo.LastWriteTimeUtc,
IsTextFile = Config.TextFileExtensions.Contains(fileInfo.Extension),
});
}
return result;
}
private FileInfo GetPath(Files request)
{
return new FileInfo(Path.Combine(this.Config.RootDirectory, request.Path.GetSafePath()));
}
private FileInfo GetAndValidateExistingPath(Files request)
{
var targetFile = GetPath(request);
if (!targetFile.Exists && !Directory.Exists(targetFile.FullName))
throw new HttpError(HttpStatusCode.NotFound, new FileNotFoundException("Could not find: " + request.Path));
return targetFile;
}
private FileResult GetFileResult(FileInfo fileInfo)
{
var isTextFile = this.Config.TextFileExtensions.Contains(fileInfo.Extension);
return new FileResult
{
Name = fileInfo.Name,
Extension = fileInfo.Extension,
FileSizeBytes = fileInfo.Length,
IsTextFile = isTextFile,
Contents = isTextFile ? File.ReadAllText(fileInfo.FullName) : null,
ModifiedDate = fileInfo.LastWriteTimeUtc,
};
}
}
}