Skip to content

Commit 34a2ff2

Browse files
author
Ben Stahl
committed
Implement creation-defer-length extension for FileStore
1 parent 6446b96 commit 34a2ff2

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

lib/models/File.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Uid = require('./Uid');
1111
class File {
1212
constructor(upload_length, upload_defer_length) {
1313
if (upload_length === undefined && upload_defer_length === undefined) {
14-
throw new Error('[File] constructor must be given either a upload_length or upload_defer_length')
14+
throw new Error('[File] constructor must be given either a upload_length or upload_defer_length');
1515
}
1616
this.upload_length = upload_length;
1717
this.upload_defer_length = upload_defer_length;

lib/stores/FileStore.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FileStore extends DataStore {
2222

2323
this.directory = options.directory || options.path.replace(/^\//, '');
2424

25-
this.extensions = ['creation'];
25+
this.extensions = ['creation', 'creation-defer-length'];
2626
this.database = new Configstore(`${pkg.name}-${pkg.version}`);
2727
this._checkOrCreateDirectory();
2828
}
@@ -53,7 +53,7 @@ class FileStore extends DataStore {
5353
return reject(err);
5454
}
5555

56-
this.database.set(`${file.id}.upload_length`, file.upload_length);
56+
this.database.set(file.id, file);
5757

5858
return fs.close(fd, (exception) => {
5959
if (exception) {
@@ -70,13 +70,13 @@ class FileStore extends DataStore {
7070
* Write to the file, starting at the provided offset
7171
*
7272
* @param {object} req http.incomingMessage
73-
* @param {string} file_name Name of file
73+
* @param {string} file_id Name of file
7474
* @param {integer} offset starting offset
7575
* @return {Promise}
7676
*/
77-
write(req, file_name, offset) {
77+
write(req, file_id, offset) {
7878
return new Promise((resolve, reject) => {
79-
const path = `${this.directory}/${file_name}`;
79+
const path = `${this.directory}/${file_id}`;
8080
const options = {
8181
flags: 'r+',
8282
start: offset,
@@ -112,16 +112,16 @@ class FileStore extends DataStore {
112112
/**
113113
* Return file stats, if they exits
114114
*
115-
* @param {string} file_name name of the file
115+
* @param {string} file_id name of the file
116116
* @return {object} fs stats
117117
*/
118-
getOffset(file_name) {
119-
const db_record = this.database.get(file_name);
118+
getOffset(file_id) {
119+
const db_record = this.database.get(file_id);
120120
return new Promise((resolve, reject) => {
121-
const file_path = `${this.directory}/${file_name}`;
121+
const file_path = `${this.directory}/${file_id}`;
122122
fs.stat(file_path, (error, stats) => {
123123
if (error && error.code === FILE_DOESNT_EXIST && db_record) {
124-
console.warn(`[FileStore] getOffset: No file found at ${file_path} but db record exists ${db_record.toString()}`);
124+
console.warn(`[FileStore] getOffset: No file found at ${file_path} but db record exists`, db_record);
125125
return reject(410);
126126
}
127127

@@ -134,8 +134,12 @@ class FileStore extends DataStore {
134134
console.warn(error);
135135
return reject(error);
136136
}
137-
stats.upload_length = this.database.get(`${file_name}.upload_length`);
138-
return resolve(stats);
137+
138+
return resolve({
139+
size: stats.size,
140+
upload_length: db_record.upload_length,
141+
upload_defer_length: db_record.upload_defer_length,
142+
});
139143
});
140144
});
141145
}

0 commit comments

Comments
 (0)