/
upload.ts
549 lines (463 loc) · 14.5 KB
/
upload.ts
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import * as fs from 'fs';
import * as path from 'path';
import * as rootPath from 'app-root-path';
import * as knex from 'knex';
import * as mv from 'mv';
import Editor from './editor';
import Field from './field';
import promisify from './promisify';
let stat = promisify( fs.stat );
let readFile = promisify( fs.readFile );
let rename = promisify( mv );
export enum DbOpts {
Content,
ContentType,
Extn,
Name,
FileName, // Name + Extn
FileSize,
MimeType,
ReadOnly,
SystemPath
// Note that the PHP and .NET libraries have a WebPath, but that isn't
// available here as there isn't a good and reliable way to get the web
// root in node (it could be anywhere!).
}
export interface IFile {
uuid: string;
field: string;
file: string; // full path
filename: string; // name + extn
encoding: string;
mimetype: string;
truncated: boolean;
done: boolean;
size: number; // Added
extn: string; // Added
name: string; // Added
}
export interface IUpload {
upload: IFile;
}
/**
* Upload class for Editor. This class provides the ability to easily specify
* file upload information, specifically how the file should be recorded on
* the server (database and file system).
*
* An instance of this class is attached to a field using the {@link
* Field.upload} method. When Editor detects a file upload for that file the
* information provided for this instance is executed.
*
* The configuration is primarily driven through the {@link db} and {@link
* action} methods:
*
* * {@link db} Describes how information about the uploaded file is to be
* stored on the database.
* * {@link action} Describes where the file should be stored on the file system
* and provides the option of specifying a custom action when a file is
* uploaded.
*
* Both methods are optional - you can store the file on the server using the
* {@link db} method only if you want to store the file in the database, or if
* you don't want to store relational data on the database us only {@link
* action}. However, the majority of the time it is best to use both - store
* information about the file on the database for fast retrieval (using a {@link
* Editor.leftJoin()} for example) and the file on the file system for direct
* web access.
*
* @export
* @class Upload
*/
export default class Upload {
public static Db = DbOpts; // legacy
public static DbOpts = DbOpts;
private _action: string|Function;
private _dbCleanCallback; // async function
private _dbCleanTableField: string;
private _dbTable: string;
private _dbPkey: string;
private _dbFields;
private _error: string;
private _validators = [];
private _where = [];
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor
*/
constructor( action: string|Function = null ) {
if ( action ) {
this.action( action );
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
*/
/**
* Set the action to take when a file is uploaded. This can be either of:
*
* * A string - the value given is the full system path to where the
* uploaded file is written to. The value given can include three "macros"
* which are replaced by the script dependent on the uploaded file:
* * `__EXTN__` - the file extension
* * `__NAME__` - the uploaded file's name (including the extension)
* * `__ID__` - Database primary key value if the {@link db} method is
* used.
* * A closure - if a function is given the responsibility of what to do
* with the uploaded file is transferred to this function. That will
* typically involve writing it to the file system so it can be used
* later.
*
* @param {(string|Function)} action Upload action
* @returns {Upload} Self for chaining
*/
public action( action: string|Function ): Upload {
this._action = action;
return this;
}
/**
* Database configuration method. When used, this method will tell Editor
* what information you want written to a database on file upload, should
* you wish to store relational information about your file on the database
* (this is generally recommended).
*
* @param {string} table The name of the table where the file information
* should be stored
* @param {string} pkey Primary key column name. The `Upload` class
* requires that the database table have a single primary key so each
* row can be uniquely identified.
* @param {object} fields A list of the fields to be written to on upload.
* The property names are the database columns and the values can be
* defined by the constants of this class. The value can also be a
* string or a closure function if you wish to send custom information
* to the database.
* @returns {Upload} Self for chaining
*/
public db( table: string, pkey: string, fields: object ): Upload {
this._dbTable = table;
this._dbPkey = pkey;
this._dbFields = fields;
return this;
}
/**
* Set a callback function that is used to remove files which no longer have
* a reference in a source table.
*
* @param {(string|Function)} tableField Table field to be used for the delete match
* @param {Function} [callback=null] Function that will be executed on clean. It is
* given an array of information from the database about the orphaned
* rows, and can return true to indicate that the rows should be
* removed from the database. Any other return value (including none)
* will result in the records being retained.
* @returns {Upload} Self for chaining
*/
public dbClean( tableField: string|Function, callback: Function = null ): Upload {
// Argument swapping
if ( typeof tableField === 'function' ) {
this._dbCleanTableField = null;
this._dbCleanCallback = tableField;
}
else {
this._dbCleanTableField = tableField;
this._dbCleanCallback = callback;
}
return this;
}
/**
* Add a validation method to check file uploads. Multiple validators can be
* added by calling this method multiple times - they will be executed in
* sequence when a file has been uploaded.
*
* @param {any} fn Validation function. A files parameter is
* passed in for the uploaded file and the return is either a string
* (validation failed and error message), or `true` (validation passed).
* @returns {Upload} Self for chaining
*/
public validator( fn ): Upload {
this._validators.push( fn );
return this;
}
/**
* Add a condition to the data to be retrieved from the database. This
* must be given as a function to be executed (usually anonymous) and
* will be passed in a single argument, the `Query` object, to which
* conditions can be added. Multiple calls to this method can be made.
*
* @param {any} fn Knex WHERE condition
* @returns {Upload} Self for chaining
*/
public where( fn ): Upload {
this._where.push( fn );
return this;
}
/* * * * * * * * * * * * * * * * * * * * * * * * *
* Internal methods
*/
/**
* @ignore
*/
public async data( db: knex, ids: string[] = null ): Promise<object> {
if ( ! this._dbTable ) {
return null;
}
// Select the details requested, for the columns requested
let query = db
.select( this._dbPkey )
.from( this._dbTable );
let keys = Object.keys( this._dbFields );
for ( let i = 0, ien = keys.length ; i < ien ; i++ ) {
let key = keys[i];
if ( this._dbFields[ key ] !== DbOpts.Content ) {
query.select( key );
}
}
if ( ids !== null ) {
query.whereIn( this._dbPkey, ids );
}
for ( let i = 0, ien = this._where.length ; i < ien ; i++ ) {
query.where( this._where[i] );
}
let result = await query;
let out = {};
for ( let i = 0, ien = result.length ; i < ien ; i++ ) {
out[ result[i][ this._dbPkey] ] = result[i];
}
return out;
}
/**
* @ignore
*/
public async dbCleanExec( editor: Editor, field: Field ): Promise<void> {
// Database and file system clean up BEFORE adding the new file to
// the db, otherwise it will be removed immediately
let tables = editor.table();
this._dbClean( editor.db(), tables[0], field.dbField() );
}
/**
* @ignore
*/
public error() {
return this._error;
}
/**
* @ignore
*/
public async exec( editor: Editor, upload: IUpload ): Promise <string> {
let id;
// Add any extra information to the upload structure
let fileInfo = await stat( upload.upload.file );
upload.upload.size = fileInfo.size;
let a = upload.upload.filename.split('.');
upload.upload.extn = a.length > 1 ?
a.pop() :
'';
upload.upload.name = a.join('.');
// Validation
for ( let i = 0, ien = this._validators.length ; i < ien ; i++ ) {
let result = await this._validators[i]( upload.upload );
if ( typeof result === 'string' ) {
this._error = result;
return null;
}
}
// Database
if ( this._dbTable ) {
let fields = Object.keys( this._dbFields );
for ( let i = 0, ien = fields.length ; i < ien ; i++ ) {
let prop = this._dbFields[ fields[i] ];
// We can't know what the path is, if it has moved into place
// by an external function - throw an error if this does happen
if ( typeof this._action !== 'string' && prop === DbOpts.SystemPath ) {
this._error = 'Cannot set path information in the database ' +
'if a custom method is used to save the file.';
return null;
}
}
// Commit to the database
id = await this._dbExec( editor.db(), upload );
}
let res = await this._actionExec( id, upload );
return res;
}
/**
* @ignore
*/
public pkey() {
return this._dbPkey;
}
/**
* @ignore
*/
public table() {
return this._dbTable;
}
/* * * * * * * * * * * * * * * * * * * * * * * * *
* Private methods
*/
private async _actionExec( id: string, files: IUpload ): Promise<string> {
if ( typeof this._action === 'function' ) {
let res = await this._action( files.upload, id );
return res;
}
// Default action - move the file to the location specified by the
// action string
let to = this._substitute( this._action, files.upload.file, id );
to = path.normalize( to );
try {
await( rename( files.upload.file, to, {mkdirp: true} ) );
} catch (e) {
this._error = 'An error occurred while moving the uploaded file.';
return null;
}
return id !== null ?
id :
to;
}
private async _dbClean( db: knex, editorTable: string, fieldName: string ): Promise<void> {
let callback = this._dbCleanCallback;
let that = this;
if ( ! this._dbTable || ! callback ) {
return;
}
// If there is a table / field that we should use to check if the value
// is in use, then use that. Otherwise we'll try to use the information
// from the Editor / Field instance.
if ( this._dbCleanTableField ) {
fieldName = this._dbCleanTableField;
}
let table;
let field;
let a = fieldName.split('.');
if ( a.length === 1 ) {
table = editorTable;
field = a[0];
}
else if ( a.length === 2 ) {
table = a[0];
field = a[1];
}
else {
table = a[1];
field = a[2];
}
// Select the details requested, for the columns requested
let fields = this._dbFields;
let columns = Object.keys( fields );
let query = db
.select( this._dbPkey )
.from( this._dbTable );
for ( let i = 0, ien = columns.length ; i < ien ; i++ ) {
let column = columns[i];
let prop = fields[ column ];
if ( prop !== DbOpts.Content ) {
query.select( column );
}
}
query.whereNotIn( this._dbPkey, function() {
this.select( field ).from( table ).whereNotNull( field );
} );
let rows = await query;
if ( rows.length === 0 ) {
return;
}
let result = await callback( rows );
// Delete the selected rows, iff the developer says to do so with the
// returned value (i.e. acknowledge that the files have be removed from
// the file system)
if ( result === true ) {
let queryDel = db
.from( this._dbTable )
.where( function() {
for ( let i = 0, ien = rows.length ; i < ien ; i++ ) {
this.orWhere( { [that._dbPkey]: rows[i][that._dbPkey] } );
}
} );
await queryDel.del();
}
}
private async _dbExec( db: knex, files: IUpload ): Promise<string> {
let pathFields = {};
let fields = this._dbFields;
let columns = Object.keys( fields );
let set = {};
let upload = files.upload;
for ( let i = 0, ien = columns.length ; i < ien ; i++ ) {
let column = columns[i];
let prop = fields[ column ];
switch ( prop ) {
case DbOpts.ReadOnly:
break;
case DbOpts.Content:
set[ column ] = await readFile( upload.file );
break;
case DbOpts.ContentType:
case DbOpts.MimeType:
set[ column ] = upload.mimetype;
break;
case DbOpts.Extn:
set[ column ] = upload.extn;
break;
case DbOpts.FileName:
set[ column ] = upload.filename;
break;
case DbOpts.Name:
set[ column ] = upload.name;
break;
case DbOpts.FileSize:
set[ column ] = upload.size;
break;
case DbOpts.SystemPath:
pathFields[ column ] = this._action;
set[ column ] = '-'; // Use a temporary value to avoid cases
break; // where the db will reject empty values
default:
let val = typeof prop === 'function' ?
prop( db, upload ) :
prop;
if ( typeof val === 'string' && val.match(/\{.*\}/) ) {
pathFields[ column ] = val;
set[ column ] = '-';
}
else {
set[ column ] = val;
}
break;
}
}
let res = await db
.insert( set )
.from( this._dbTable )
.returning( this._dbPkey );
let id = res[0];
// Update the newly inserted row with the path information. We have to
// use a second statement here as we don't know in advance what the
// database schema is and don't want to prescribe that certain triggers
// etc be created. It makes it a bit less efficient but much more
// compatible
let pathKeys = Object.keys( pathFields );
if ( pathKeys.length ) {
// For this to operate the action must be a string, which is
// validated in the `exec` method
let toSet = {};
for ( let i = 0, ien = pathKeys.length ; i < ien ; i++ ) {
let key = pathKeys[i];
toSet[ key ] = this._substitute( pathFields[key], upload.file, id );
}
await db
.update( toSet )
.from( this._dbTable )
.where( { [this._dbPkey]: id } );
}
return id;
}
private _substitute( convert: string, uploadPath: string, id: string ): string {
let a = uploadPath.toString().split( '/' );
let fileName = a.pop();
let fileParts = fileName.split('.');
let extn = fileParts.pop();
let namePart = fileParts.join('.');
let to = convert.toString();
to = to.replace( '{name}', namePart );
to = to.replace( '{id}', id );
to = to.replace( '{extn}', extn );
return to;
}
}