Skip to content

Commit

Permalink
Implement creation-defer-length extension for FileStore
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Stahl committed May 18, 2016
1 parent 6446b96 commit 34a2ff2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/models/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Uid = require('./Uid');
class File {
constructor(upload_length, upload_defer_length) {
if (upload_length === undefined && upload_defer_length === undefined) {
throw new Error('[File] constructor must be given either a upload_length or upload_defer_length')
throw new Error('[File] constructor must be given either a upload_length or upload_defer_length');
}
this.upload_length = upload_length;
this.upload_defer_length = upload_defer_length;
Expand Down
28 changes: 16 additions & 12 deletions lib/stores/FileStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FileStore extends DataStore {

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

this.extensions = ['creation'];
this.extensions = ['creation', 'creation-defer-length'];
this.database = new Configstore(`${pkg.name}-${pkg.version}`);
this._checkOrCreateDirectory();
}
Expand Down Expand Up @@ -53,7 +53,7 @@ class FileStore extends DataStore {
return reject(err);
}

this.database.set(`${file.id}.upload_length`, file.upload_length);
this.database.set(file.id, file);

return fs.close(fd, (exception) => {
if (exception) {
Expand All @@ -70,13 +70,13 @@ class FileStore extends DataStore {
* Write to the file, starting at the provided offset
*
* @param {object} req http.incomingMessage
* @param {string} file_name Name of file
* @param {string} file_id Name of file
* @param {integer} offset starting offset
* @return {Promise}
*/
write(req, file_name, offset) {
write(req, file_id, offset) {
return new Promise((resolve, reject) => {
const path = `${this.directory}/${file_name}`;
const path = `${this.directory}/${file_id}`;
const options = {
flags: 'r+',
start: offset,
Expand Down Expand Up @@ -112,16 +112,16 @@ class FileStore extends DataStore {
/**
* Return file stats, if they exits
*
* @param {string} file_name name of the file
* @param {string} file_id name of the file
* @return {object} fs stats
*/
getOffset(file_name) {
const db_record = this.database.get(file_name);
getOffset(file_id) {
const db_record = this.database.get(file_id);
return new Promise((resolve, reject) => {
const file_path = `${this.directory}/${file_name}`;
const file_path = `${this.directory}/${file_id}`;
fs.stat(file_path, (error, stats) => {
if (error && error.code === FILE_DOESNT_EXIST && db_record) {
console.warn(`[FileStore] getOffset: No file found at ${file_path} but db record exists ${db_record.toString()}`);
console.warn(`[FileStore] getOffset: No file found at ${file_path} but db record exists`, db_record);
return reject(410);
}

Expand All @@ -134,8 +134,12 @@ class FileStore extends DataStore {
console.warn(error);
return reject(error);
}
stats.upload_length = this.database.get(`${file_name}.upload_length`);
return resolve(stats);

return resolve({
size: stats.size,
upload_length: db_record.upload_length,
upload_defer_length: db_record.upload_defer_length,
});
});
});
}
Expand Down

0 comments on commit 34a2ff2

Please sign in to comment.