Some fixes after refactoring

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