-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIController.cs
179 lines (150 loc) · 6.03 KB
/
APIController.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using HttpMultipartParser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Constants;
using Unosquare.Labs.EmbedIO.Modules;
using Unosquare.Swan;
namespace EmbedStorage
{
public class APIController : Controller
{
public APIController(IHttpContext context) : base(context)
{
}
private string GetMimeByExtention(string ext)
{
if (string.IsNullOrEmpty(ext)) return "application/octet-stream";
if (!MimeTypes.DefaultMimeTypes.ContainsKey(ext))
{
return "application/octet-stream";
}
return MimeTypes.DefaultMimeTypes[ext];
}
private async Task<bool> WrapFileAccess(Func<Task<bool>> access)
{
try
{
return await access();
}
catch (FormatException ex)
{
Terminal.Error(ex, ex.Source, ex.Message);
return await Ok(new { error = -20, status = "file not exist" });
}
catch (System.IO.FileNotFoundException ex)
{
Terminal.Error(ex, ex.Source, ex.Message);
Response.StatusCode = 404;
return await Ok(new { error = -21, status = "file not exist" });
}
catch (ArgumentNullException ex)
{
Terminal.Error(ex, ex.Source, ex.Message);
Response.StatusCode = 404;
return await Ok(new { error = -22, status = "file not exist" });
}
catch (System.IO.IOException ex)
{
Terminal.Error(ex, ex.Source, ex.Message);
Response.StatusCode = 503;
return await Ok(new { error = -23, status = "resource busy" });
}
catch (Exception ex)
{
Response.StatusCode = 500;
return await Error(ex);
}
}
[WebApiHandler(HttpVerbs.Get, "/api/file/{fileid}")]
public async Task<bool> File(string fileid)
{
return await WrapFileAccess(() =>
{
Guid guid = Guid.Parse(fileid);
StorageModel storage = Program.FileStorages.FirstOrDefault(s => s.FileId == guid);
if (storage == null)
{
throw new System.IO.FileNotFoundException();
}
var fs = Program.GetWorkFileSystem().OpenFile(storage.Path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
Response.ContentType = GetMimeByExtention(storage.Extention);
return Response.BinaryResponseAsync(fs, false);
});
}
[WebApiHandler(HttpVerbs.Get, "/api/download/{fileid}")]
public async Task<bool> Download(string fileid)
{
return await WrapFileAccess(() =>
{
Guid guid = Guid.Parse(fileid);
StorageModel storage = Program.FileStorages.FirstOrDefault(s => s.FileId == guid);
if (storage == null)
{
throw new System.IO.FileNotFoundException();
}
var fs = Program.GetWorkFileSystem().OpenFile(storage.Path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Net.WebUtility.UrlEncode(storage.FileName));
Response.AddHeader("Content-Length", storage.FileLength.ToString());
Response.ContentType = GetMimeByExtention(storage.Extention);
return Response.BinaryResponseAsync(fs, false);
});
}
[WebApiHandler(HttpVerbs.Post, "/api/upload")]
public async Task<bool> Upload()
{
try
{
var parser = new MultipartFormDataParser(Request.InputStream);
if (parser.Files.Any() == false)
{
return await Ok(new { error = -1, status = "no input file." });
}
var file = parser.Files[0];
DateTime now = DateTime.Now;
string dir = "/data/" + now.Year + "/" + now.Month + "/" + now.Day;
if (!Program.GetWorkFileSystem().DirectoryExists(dir))
{
Program.GetWorkFileSystem().CreateDirectory(dir);
}
Guid fileid = Guid.NewGuid();
string ext = System.IO.Path.GetExtension(file.FileName);
string filepath = dir + "/" + fileid + ext;
using (var fs = Program.GetWorkFileSystem().OpenFile(filepath,
System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.ReadWrite))
{
await file.Data.CopyToAsync(fs);
}
var storage = new StorageModel()
{
DateExpires = DateTime.Now.AddYears(20),
Extention = ext,
FileId = fileid,
FileLength = file.Data.Length,
FileName = file.FileName,
UploadDate = DateTime.Now,
Path = filepath
};
Program.FileStorages.Add(storage);
bool rs = Program.Save();
if (rs)
{
return await Ok(new { error = 0, status = "ok.", fileid = fileid });
}
else
{
return await Ok(new { error = -2, status = "write database fail.", fileid = fileid });
}
}
catch (Exception ex)
{
Terminal.Error(ex, ex.Source, ex.Message);
return await Ok(new { error = -3, status = "upload fail.", fileid = "" });
}
}
}
}