Some fixes after refactoring

parent c1f0080f
......@@ -5,7 +5,7 @@ const Promise = require('bluebird');
const logging = require('./logging').getWrapperForModule('cleaner');
const Metadata = require('./metadata');
const redis = require('./redis');
const Ipfs = require('./ipfs');
const Ipfs = require('./storage-backends/ipfs');
/**
* Represents a cleaner for attachments.
......
......@@ -3,7 +3,7 @@
const Promise = require('bluebird');
const { FileNotFoundError } = require('../errors');
const logging = request('../logging').getWrapperForModule('download');
const logging = require('../logging').getWrapperForModule('download');
/**
* Creates route handler for /<base-url>/:hash/:filename.
......@@ -40,6 +40,7 @@ module.exports = function (storage, metadataStorage, options) {
};
let handleError = (error) => {
console.log(error);
if (error instanceof FileNotFoundError) {
response.sendStatus(404);
} else {
......
......@@ -2,7 +2,7 @@
const Promise = require('bluebird');
const FileNotFoundError = require('../errors').FileNotFoundError;
const { FileNotFoundError } = require('../errors');
const logging = require('../logging').getWrapperForModule('download_thumb');
/**
......
......@@ -48,10 +48,7 @@ module.exports = function (storage, metadataStorage, options) {
return storage.add(file);
};
let addMetadata = (result) => {
result = result[0];
hash = result.hash;
let addMetadata = (hash) => {
return metadataStorage.addRecord(hash, mimetype, size);
};
......@@ -81,5 +78,3 @@ module.exports = function (storage, metadataStorage, options) {
.catch((error) => logging.error(error));
};
};
module.exports = upload;
......@@ -23,10 +23,12 @@ class FilesystemStorage {
add (file) {
logging.verbose(`Uploading '${file.originalFilename}' to filesystem`);
const hash = this.computeHash(file);
const filePath = this.getFilePath(hash);
return fs.outputFile(filePath, file).then(() => hash);
return fs.readFile(file.path).then(content => {
const hash = this.computeHash(content);
const filePath = this.getFilePath(hash);
return fs.outputFile(filePath, content).then(() => hash);
});
}
/**
......@@ -45,13 +47,13 @@ class FilesystemStorage {
/**
* Gets file from IPFS.
* @param {String} hash IPFS hash.
* @return {Promise<ReadableStream>} Readable Stream of attachment.
* @return {ReadableStream} Readable Stream of attachment.
*/
get (hash) {
logging.verbose(`Serving attachment for ${hash} from filesystem`);
const filePath = this.getFilePath(hash);
return fs.readFile(filePath);
return fs.createReadStream(filePath);
}
......@@ -66,4 +68,6 @@ class FilesystemStorage {
const fileName = hash.slice(2);
return path.join(this.basePath, dirName, fileName);
}
}
\ No newline at end of file
}
module.exports = FilesystemStorage;
......@@ -32,7 +32,8 @@ class IpfsStorage {
let hash;
return this.ipfs.files.add(files).then((res) => {
hash = res;
hash = res[0].hash;
return this.pin(hash);
}).then(() => {
return Promise.resolve(hash);
......
......@@ -2,10 +2,9 @@
const path = require('path');
let mods = [
const mods = [
'cleaner',
'errors',
'ipfs',
'logging',
'metadata',
'pin',
......@@ -14,7 +13,18 @@ let mods = [
'thumbnail',
];
module.exports = {};
module.exports = {
handlers: {
delAttachment: require('./components/handlers/del_attachment'),
download: require('./components/handlers/download'),
downloadThumb: require('./components/handlers/download_thumb'),
upload: require('./components/handlers/upload'),
},
storageBackends: {
FilesystemStorage: require('./components/storage-backends/filesystem'),
IpfsStorage: require('./components/storage-backends/ipfs'),
},
};
mods.forEach((mod) => {
module.exports[mod] = require(
......
......@@ -9,8 +9,9 @@
"private": true,
"dependencies": {
"bluebird": "3.5.0",
"fs-extra": "2.1.2",
"fs-extra": "^6.0.0",
"ipfs-api": "14.0.0",
"multiparty": "^4.1.3",
"redis": "2.7.1",
"sharp": "0.17.3",
"winston": "2.3.1"
......
......@@ -42,7 +42,6 @@ describe('Ipfs', () => {
it('should return hash', function (done) {
this.timeout(30000);
ipfs.add(file).then((hash) => {
hash = hash[0].hash;
hash.should.startWith('Qm');
hash.should.have.length(46);
done();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment