Changeset View
Changeset View
Standalone View
Standalone View
lib/app/file-manager.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const Promise = require("bluebird"); | |||||
const UUIDGenerator = require("shortid"); | const UUIDGenerator = require("shortid"); | ||||
const path = require("path"); | const path = require("path"); | ||||
const fs = require("fs"); | const fs = require("fs"); | ||||
const { promisify } = require("util"); | |||||
const exists = promisify(fs.exists); | |||||
const mkdir = promisify(fs.mkdir); | |||||
const writeFile = promisify(fs.writeFile); | |||||
const File = locreq("lib/data-structures/file.js"); | const File = locreq("lib/data-structures/file.js"); | ||||
function FileManager(datastore, logger, upload_path) { | class FileManager { | ||||
constructor(datastore, logger, upload_path) { | |||||
this.datastore = datastore; | this.datastore = datastore; | ||||
this.logger = logger; | this.logger = logger; | ||||
this.upload_path = upload_path; | this.upload_path = upload_path; | ||||
if (!fs.existsSync(this.upload_path)) { | |||||
fs.mkdirSync(this.upload_path); | |||||
} | } | ||||
async init() { | |||||
const dirExists = await exists(this.upload_path); | |||||
if (!dirExists) await mkdir(this.upload_path); | |||||
} | } | ||||
FileManager.pure = { | async save_file(file) { | ||||
save_file: function(datastore, upload_path, file) { | |||||
const newID = UUIDGenerator(); | const newID = UUIDGenerator(); | ||||
const upload_path_with_sealious_name = `${upload_path}/${newID}`; | const upload_path_with_sealious_name = `${this.upload_path}/${newID}`; | ||||
return Promise.promisify(fs.writeFile)( | await writeFile(upload_path_with_sealious_name, file.data); | ||||
upload_path_with_sealious_name, | |||||
file.data | await this.datastore.insert( | ||||
) | "files", | ||||
.then(function() { | { | ||||
const file_database_entry = { | |||||
original_name: file.filename, | original_name: file.filename, | ||||
creation_context: file.context, | creation_context: file.context, | ||||
id: newID, | id: newID, | ||||
mime_type: file.mime, | mime_type: file.mime, | ||||
}; | }, | ||||
return datastore.insert("files", file_database_entry, {}); | {} | ||||
}) | ); | ||||
.then(function() { | |||||
return { | return { | ||||
id: newID, | id: newID, | ||||
filename: file.filename, | filename: file.filename, | ||||
}; | }; | ||||
}); | } | ||||
}, | |||||
find: function(datastore, upload_path, context, query) { | async find(context, query) { | ||||
return datastore.find("files", query).then(function(documents) { | const documents = await this.datastore.find("file", query); | ||||
const parsed_documents = documents.map(function(document) { | const parsed_documents = documents.map(doc => { | ||||
const ret = File.from_db_entry(document); | const ret = File.from_db_entry(doc); | ||||
ret.path_on_hdd = path.resolve(upload_path, `./${ret.id}`); | ret.path_on_hdd = path.resolve(this.upload_path, `./${ret.id}`); | ||||
return ret; | return ret; | ||||
}); | }); | ||||
return Promise.resolve(parsed_documents); | return parsed_documents; | ||||
}); | } | ||||
}, | } | ||||
get_by_id: function(datastore, upload_path, context, file_id) { | |||||
return FileManager.pure | |||||
.find(datastore, upload_path, context, { id: file_id }) | |||||
.then(function(file_array) { | |||||
return Promise.resolve(file_array[0]); | |||||
}); | |||||
}, | |||||
}; | |||||
FileManager.prototype = { | |||||
save_file(file) { | |||||
return FileManager.pure.save_file( | |||||
this.datastore, | |||||
this.upload_path, | |||||
file | |||||
); | |||||
}, | |||||
find(context, query) { | |||||
return FileManager.pure.find( | |||||
this.datastore, | |||||
this.upload_path, | |||||
context, | |||||
query | |||||
); | |||||
}, | |||||
}; | |||||
module.exports = FileManager; | module.exports = FileManager; |