/
FileStore.js
184 lines (157 loc) · 5.75 KB
/
FileStore.js
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
'use strict';
const DataStore = require('./DataStore');
const File = require('../models/File');
const fs = require('fs');
const Configstore = require('configstore');
const pkg = require('../../package.json');
const MASK = '0777';
const IGNORED_MKDIR_ERROR = 'EEXIST';
const FILE_DOESNT_EXIST = 'ENOENT';
const ERRORS = require('../constants').ERRORS;
const EVENTS = require('../constants').EVENTS;
const debug = require('debug');
const log = debug('tus-node-server:stores:filestore');
/**
* @fileOverview
* Store using local filesystem.
*
* @author Ben Stahl <bhstahl@gmail.com>
*/
class FileStore extends DataStore {
constructor(options) {
super(options);
this.directory = options.directory || options.path.replace(/^\//, '');
this.extensions = ['creation', 'creation-defer-length'];
this.configstore = new Configstore(`${pkg.name}-${pkg.version}`);
this._checkOrCreateDirectory();
}
/**
* Ensure the directory exists.
*/
_checkOrCreateDirectory() {
fs.mkdir(this.directory, MASK, (error) => {
if (error && error.code !== IGNORED_MKDIR_ERROR) {
throw error;
}
});
}
/**
* Create an empty file.
*
* @param {object} req http.incomingMessage
* @param {File} file
* @return {Promise}
*/
create(req) {
return new Promise((resolve, reject) => {
const upload_length = req.headers['upload-length'];
const upload_defer_length = req.headers['upload-defer-length'];
const upload_metadata = req.headers['upload-metadata'];
if (upload_length === undefined && upload_defer_length === undefined) {
return reject(ERRORS.INVALID_LENGTH);
}
let file_id;
try {
file_id = this.generateFileName(req);
}
catch (generateError) {
log('[FileStore] create: check your namingFunction. Error', generateError);
return reject(ERRORS.FILE_WRITE_ERROR);
}
const file = new File(file_id, upload_length, upload_defer_length, upload_metadata);
return fs.open(`${this.directory}/${file.id}`, 'w', (err, fd) => {
if (err) {
log('[FileStore] create: Error', err);
return reject(err);
}
this.configstore.set(file.id, file);
return fs.close(fd, (exception) => {
if (exception) {
log('[FileStore] create: Error', exception);
return reject(exception);
}
this.emit(EVENTS.EVENT_FILE_CREATED, { file });
return resolve(file);
});
});
});
}
/** Get file from filesystem
*
* @param {string} file_id Name of the file
*
* @return {stream.Readable}
*/
read(file_id) {
const path = `${this.directory}/${file_id}`;
return fs.createReadStream(path);
}
/**
* Write to the file, starting at the provided offset
*
* @param {object} req http.incomingMessage
* @param {string} file_id Name of file
* @param {integer} offset starting offset
* @return {Promise}
*/
write(req, file_id, offset) {
return new Promise((resolve, reject) => {
const path = `${this.directory}/${file_id}`;
const options = {
flags: 'r+',
start: offset,
};
const stream = fs.createWriteStream(path, options);
let new_offset = 0;
req.on('data', (buffer) => {
new_offset += buffer.length;
});
stream.on('error', (e) => {
log('[FileStore] write: Error', e);
reject(ERRORS.FILE_WRITE_ERROR);
});
return req.pipe(stream).on('finish', () => {
log(`[FileStore] write: ${new_offset} bytes written to ${path}`);
offset += new_offset;
log(`[FileStore] write: File is now ${offset} bytes`);
const config = this.configstore.get(file_id);
if (config && parseInt(config.upload_length, 10) === offset) {
this.emit(EVENTS.EVENT_UPLOAD_COMPLETE, { file: config });
}
resolve(offset);
});
});
}
/**
* Return file stats, if they exits
*
* @param {string} file_id name of the file
* @return {object} fs stats
*/
getOffset(file_id) {
const config = this.configstore.get(file_id);
return new Promise((resolve, reject) => {
const file_path = `${this.directory}/${file_id}`;
fs.stat(file_path, (error, stats) => {
if (error && error.code === FILE_DOESNT_EXIST && config) {
log(`[FileStore] getOffset: No file found at ${file_path} but db record exists`, config);
return reject(ERRORS.FILE_NO_LONGER_EXISTS);
}
if (error && error.code === FILE_DOESNT_EXIST) {
log(`[FileStore] getOffset: No file found at ${file_path}`);
return reject(ERRORS.FILE_NOT_FOUND);
}
if (error) {
return reject(error);
}
if (stats.isDirectory()) {
log(`[FileStore] getOffset: ${file_path} is a directory`);
return reject(ERRORS.FILE_NOT_FOUND);
}
const data = Object.assign(stats, config);
return resolve(data);
});
});
}
}
module.exports = FileStore;