forked from zamronypj/fano
/
UploadedFileImpl.pas
276 lines (242 loc) · 9.24 KB
/
UploadedFileImpl.pas
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
{*!
* Fano Web Framework (https://fanoframework.github.io)
*
* @link https://github.com/fanoframework/fano
* @copyright Copyright (c) 2018 Zamrony P. Juhara
* @license https://github.com/fanoframework/fano/blob/master/LICENSE (MIT)
*}
unit UploadedFileImpl;
interface
{$MODE OBJFPC}
{$H+}
uses
InjectableObjectImpl,
UploadedFileIntf;
type
(*!------------------------------------------------
* basic class having capability to store
* HTTP file upload
*
* @author Zamrony P. Juhara <zamronypj@yahoo.com>
*-----------------------------------------------*)
TUploadedFile = class(TInterfacedObject, IUploadedFile)
private
(*!----------------------------------------
* temporary file path
*-----------------------------------------*)
tmpFile : string;
(*!----------------------------------------
* original filename as uploaded by user
*-----------------------------------------*)
clientFilename : string;
(*!----------------------------------------
* file size
*-----------------------------------------*)
tmpFileSize : int64;
(*!----------------------------------------
* MIME type as uploaded by user
*-----------------------------------------*)
tmpMimeType : string;
public
(*!----------------------------------------
* constructor
*-----------------------------------------
* @param content content of uploaded file
* @param contentType content type of uploaded file
* @param origFilename original filename as uploaded by user
*-----------------------------------------
* We will create temporary file to hold uploaded
* file content.
*-----------------------------------------*)
constructor create(
const content : string;
const contentType : string;
const origFilename : string
);
(*!----------------------------------------
* destructor
*-----------------------------------------
* we will delete any unmoved uploaded files
*-----------------------------------------*)
destructor destroy(); override;
(*!------------------------------------------------
* move uploaded file to specified
*-------------------------------------------------
* @param string target path
* @return current instance
* @throws EInvalidOperation
*------------------------------------------------
* Implementor must raise exception when moveTo()
* called multiple time
* Implementor must check for file permission
*------------------------------------------------*)
function moveTo(const targetPath : string) : IUploadedFile;
(*!------------------------------------------------
* get uploaded file size
*-------------------------------------------------
* @return size in bytes of uploaded file
*------------------------------------------------*)
function size() : int64;
(*!------------------------------------------------
* get client side filename
*-------------------------------------------------
* @return string original filename as uploaded by client
*------------------------------------------------*)
function getClientFilename() : string;
(*!------------------------------------------------
* get client side MIME type
*-------------------------------------------------
* @return string original MIME type as uploaded by client
*------------------------------------------------*)
function getClientMediaType() : string;
end;
implementation
uses
Classes,
SysUtils,
EInvalidUploadedFileImpl;
resourcestring
sErrDuplicateTemporaryUploadedFile = 'Duplicate temporary uploaded file %s';
sErrInvalidUploadedFile = 'Invalid uploaded file. You can move uploaded file only once';
function makeRandomStr(const prefix : string; const suffix : string) : string;
begin
result := prefix + inttostr(random(MAXINT)) + suffix;
end;
(*!----------------------------------------
* Create temporary file
*-----------------------------------------
* @param content content of file
* @param prefix string prepended before random string
* @param suffix string appended after random string
* @return filename of file
*-----------------------------------------*)
function createTmpFile(
const content : string;
const prefix : string;
const suffix : string
) : string;
var fstream : TFileStream;
begin
result := getTempDir() + DirectorySeparator + makeRandomStr(prefix, suffix);
if (fileExists(result)) then
begin
//this is just pre caution, try to recreate name
result := getTempDir() + DirectorySeparator + makeRandomStr(prefix, suffix);
if (fileExists(result)) then
begin
//give up
raise EInvalidUploadedFile.createFmt(
sErrDuplicateTemporaryUploadedFile,
[ result ]
);
end;
end;
fstream := TFileStream.create(result, fmCreate);
try
fstream.write(content[1], length(content));
finally
freeAndNil(fstream);
end;
end;
(*!----------------------------------------
* move temporary file
*-----------------------------------------
* @param srcFilename source filename
* @param dstFilename destination filename
*-----------------------------------------*)
procedure moveTmpFile(const srcFilename : string; const dstFilename : string);
var srcStream, dstStream : TFileStream;
begin
srcStream := TFileStream.create(srcFilename, fmOpenRead);
dstStream := TFileStream.create(dstFilename, fmCreate);
try
dstStream.copyFrom(srcStream, srcStream.size);
finally
freeAndNil(srcStream);
freeAndNil(dstStream);
end;
end;
(*!----------------------------------------
* constructor
*-----------------------------------------
* @param content content of uploaded file
* @param contentType content type of uploaded file
* @param origFilename original filename as uploaded by user
*-----------------------------------------
* We will create temporary file to hold uploaded
* file content.
*-----------------------------------------*)
constructor TUploadedFile.create(
const content : string;
const contentType : string;
const origFilename : string
);
begin
tmpFile := createTmpFile(content, 'fano-', origFilename);
tmpFileSize := length(tmpFile);
tmpMimeType := contentType;
clientFilename := origFilename;
end;
(*!----------------------------------------
* destructor
*-----------------------------------------
* we will delete any unmoved uploaded files
*-----------------------------------------*)
destructor TUploadedFile.destroy();
begin
if (fileExists(tmpFile)) then
begin
deleteFile(tmpFile);
end;
end;
(*!------------------------------------------------
* move uploaded file to specified
*-------------------------------------------------
* @param string target path
* @return current instance
* @throws EInvalidOperation
*------------------------------------------------
* Implementor must raise exception when moveTo()
* called multiple time
* Implementor must check for file permission
*------------------------------------------------*)
function TUploadedFile.moveTo(const targetPath : string) : IUploadedFile;
begin
if (fileExists(tmpFile)) then
begin
moveTmpFile(tmpFile, targetPath);
deleteFile(tmpFile);
end else
begin
raise EInvalidUploadedFile.create(sErrInvalidUploadedFile);
end;
result := self;
end;
(*!------------------------------------------------
* get uploaded file size
*-------------------------------------------------
* @return size in bytes of uploaded file
*------------------------------------------------*)
function TUploadedFile.size() : int64;
begin
result := tmpFileSize;
end;
(*!------------------------------------------------
* get client side filename
*-------------------------------------------------
* @return string original filename as uploaded by client
*------------------------------------------------*)
function TUploadedFile.getClientFilename() : string;
begin
result := clientFilename;
end;
(*!------------------------------------------------
* get client side MIME type
*-------------------------------------------------
* @return string original MIME type as uploaded by client
*------------------------------------------------*)
function TUploadedFile.getClientMediaType() : string;
begin
result := tmpMimeType;
end;
end.