Rewrite working with gridfs

parent 9adf36ed
...@@ -9,94 +9,102 @@ const fs = require('fs'); ...@@ -9,94 +9,102 @@ const fs = require('fs');
const s2s = require('string-to-stream'); const s2s = require('string-to-stream');
const _ = require('lodash'); const _ = require('lodash');
module.exports.saveFileToDisk = (pathToSave, fileID) => { let saveStream = (bucket, filename, fileStream, metadata, options) => {
return new Promise((s, r) => {
let stream = mongoose.testingFiles.readById(fileID);
stream.on('error', e => r(e));
stream.on('close', () => s());
stream.pipe(fs.createWriteStream(pathToSave))
})
.catch(err => {
});
};
module.exports.saveFileToGrid = (path, name, meta, options) => {
return module.exports.saveStream(name, fs.createReadStream(path), meta, options);
};
module.exports.saveStream = (filename, fileStream, meta, options) => {
return (new Promise((s, r) => { return (new Promise((s, r) => {
mongoose.files.write({
filename: filename,
contentType: _.get(options, 'contentType', 'text/plain'),
metadata: meta
}, fileStream, (err, data) => {
if (err) r(err);
else s(data);
});
}))
.catch(err => {
console.log(err)
});
};
module.exports.save = (filename, fileContent, meta, options) => { let stream = bucket.createWriteStream({
return (new Promise((s, r) => { filename,
mongoose.files.write({
filename: filename,
contentType: _.get(options, 'contentType', 'text/plain'), contentType: _.get(options, 'contentType', 'text/plain'),
metadata: meta, metadata
// public: _.get(options, 'public', false),
// owner: _.get(options, 'owner', null),
// needAuth: _.get(options, 'needAuth', true)
}, s2s(fileContent), (err, data) => {
if (err) r(err);
else s(data);
}); });
fileStream.on('error', e => r(e));
fileStream.on('end', () => s(stream.id));
fileStream.pipe(stream);
})) }))
.catch(err => { .catch(err => {
console.log(err) console.log(err)
}); });
}; };
module.exports.read = (fileId) => { module.exports = bucket => {
return new Promise((s, r) => {
mongoose.files.readById(fileId, (err, data) => { return {
if (err) r(err); saveFileToDisk: (pathToSave, fileID) => {
else s(data); return new Promise((s, r) => {
}); let stream = bucket.createReadStream({_id: fileID});
}); stream.on('error', e => r(e));
stream.on('end', () => s());
}; stream.pipe(fs.createWriteStream(pathToSave));
})
module.exports.fileInfo = (fileId) => { .catch(err => {
return mongoose.files.findOne({_id: fileId}) console.log(err)
.catch(err => { });
},
});
saveFileToGrid: (path, filename, meta, options) => {
return saveStream(bucket, filename, fs.createReadStream(path), meta, options);
},
save: (filename, fileContent, metadata, options) => {
return saveStream(bucket, filename, s2s(fileContent), metadata, options)
},
saveStream: (filename, fileStream, metadata, options) => {
return saveStream(bucket, filename, fileStream, metadata, options)
},
read: fileId => {
return new Promise((s, r) => {
bucket.readFile(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
console.log(err)
});
},
readStream: fileId => {
return bucket.readById(fileId)
},
fileInfo: fileId => {
return new Promise((s, r) => {
bucket.findById(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
console.log(err)
});
},
filesInfo: async fileIds => {
let res = [];
let cursor = bucket.find({_id: {$in: fileIds}});
let document;
while ((document = await cursor.next())) {
res.push(document)
}
return res;
},
remove: fileId => {
return new Promise((s, r) => {
bucket.deleteFile(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
});
}
}
}; };
module.exports.filesInfo = (fileIds) => {
return mongoose.files.find({_id: fileIds})
.catch(err => {
});
};
module.exports.remove = (fileId) => {
return new Promise((s, r) => {
mongoose.files.unlinkById(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
});
};
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const gridfs = require('mongoose-gridfs'); const gridfs = require('mongoose-gridfs');
const logger = require('./logger')('error'); const logger = require('./logger')('error');
const files = require('./files');
mongoose.Promise = global.Promise; mongoose.Promise = global.Promise;
...@@ -26,31 +27,31 @@ module.exports = (connectionUri, relaxed) => { ...@@ -26,31 +27,31 @@ module.exports = (connectionUri, relaxed) => {
return mongoose.createConnection(connectionUri, {useNewUrlParser: true, poolSize: 25}) return mongoose.createConnection(connectionUri, {useNewUrlParser: true, poolSize: 25})
.then(connection => { .then(connection => {
module.exports = {}; module.exports = {};
module.exports.defines = require('./defines') module.exports.defines = require('./defines');
mongoose.connection = connection; mongoose.connection = connection;
module.exports.connection = connection; module.exports.connection = connection;
let files_fs = gridfs.createModel({ let files_fs = gridfs.createBucket({
modelName: 'files', bucketName: 'files',
connection: connection connection: connection
}); });
mongoose.files = files_fs; mongoose.filesBUcket = files_fs;
let testing_fs = gridfs.createModel({ let testing_fs = gridfs.createBucket({
modelName: 'testing', bucketName: 'testing',
connection: connection connection: connection
}); });
mongoose.testingFiles = testing_fs; mongoose.testingBucket = testing_fs;
let solution_fs = gridfs.createModel({ let solution_fs = gridfs.createBucket({
modelName: 'solution', bucketName: 'solution',
connection: connection connection: connection
}); });
mongoose.solutionFiles = solution_fs; mongoose.solutionBucket= solution_fs;
module.exports.courses = require('./course'); module.exports.courses = require('./course');
mongoose.courses = module.exports.courses; mongoose.courses = module.exports.courses;
...@@ -79,9 +80,9 @@ module.exports = (connectionUri, relaxed) => { ...@@ -79,9 +80,9 @@ module.exports = (connectionUri, relaxed) => {
module.exports.programs = require('./program'); module.exports.programs = require('./program');
mongoose.programs = module.exports.programs; mongoose.programs = module.exports.programs;
module.exports.fs = require('./files'); module.exports.fs = files(files_fs);
module.exports.testing_fs = require('./testing'); module.exports.testing_fs = files(testing_fs);
module.exports.solution_fs = require('./solution_files'); module.exports.solution_fs = files(solution_fs);
mongoose.fs = module.exports.fs; mongoose.fs = module.exports.fs;
mongoose.testing_fs = module.exports.testing_fs; mongoose.testing_fs = module.exports.testing_fs;
......
/**
* Created by danilaeremin on 18.07.17.
*/
"use strict";
const mongoose = require('mongoose');
const s2s = require('string-to-stream');
const _ = require('lodash');
const fs = require('fs');
module.exports.saveFileToDisk = (pathToSave, fileID) => {
return new Promise((s, r) => {
let stream = mongoose.solutionFiles.readById(fileID);
stream.on('error', e => r(e));
stream.on('close', () => s());
stream.pipe(fs.createWriteStream(pathToSave))
})
.catch(err => {
});
};
module.exports.saveFileToGrid = (path, name, meta, options) => {
return module.exports.saveStream(name, fs.createReadStream(path), meta, options);
};
module.exports.saveStream = (filename, fileStream, meta, options) => {
return (new Promise((s, r) => {
mongoose.solutionFiles.write({
filename: filename,
contentType: _.get(options, 'contentType', 'text/plain'),
metadata: meta
}, fileStream, (err, data) => {
if (err) r(err);
else s(data);
});
}))
.catch(err => {
console.log(err)
});
};
module.exports.save = (filename, fileContent, meta, options) => {
return (new Promise((s, r) => {
mongoose.solutionFiles.write({
filename: filename,
contentType: _.get(options, 'contentType', 'text/plain'),
metadata: meta,
// public: _.get(options, 'public', false),
// owner: _.get(options, 'owner', null),
// needAuth: _.get(options, 'needAuth', true)
}, s2s(fileContent), (err, data) => {
if (err) r(err);
else s(data);
});
}))
.catch(err => {
console.log(err)
});
};
module.exports.read = (fileId) => {
return new Promise((s, r) => {
mongoose.solutionFiles.readById(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
});
};
module.exports.readStream = (fileId) => {
return mongoose.solutionFiles.readById(fileId)
};
module.exports.fileInfo = (fileId) => {
return mongoose.solutionFiles.findOne({_id: fileId})
.catch(err => {
});
};
module.exports.remove = (fileId) => {
return new Promise((s, r) => {
mongoose.solutionFiles.unlinkById(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
});
};
/**
* Created by danilaeremin on 18.07.17.
*/
"use strict";
const mongoose = require('mongoose');
const s2s = require('string-to-stream');
const _ = require('lodash');
const fs = require('fs');
module.exports.saveFileToDisk = (pathToSave, fileID) => {
return new Promise((s, r) => {
let stream = mongoose.testingFiles.readById(fileID);
stream.on('error', e => r(e));
stream.on('close', () => s());
stream.pipe(fs.createWriteStream(pathToSave))
})
.catch(err => {
});
};
module.exports.saveFileToGrid = (path, name, meta, options) => {
return module.exports.saveStream(name, fs.createReadStream(path), meta, options);
};
module.exports.saveStream = (filename, fileStream, meta, options) => {
return (new Promise((s, r) => {
mongoose.testingFiles.write({
filename: filename,
contentType: _.get(options, 'contentType', 'text/plain'),
metadata: meta
}, fileStream, (err, data) => {
if (err) r(err);
else s(data);
});
}))
.catch(err => {
console.log(err)
});
};
module.exports.save = (filename, fileContent, meta, options) => {
return (new Promise((s, r) => {
mongoose.testingFiles.write({
filename: filename,
contentType: _.get(options, 'contentType', 'text/plain'),
metadata: meta
}, s2s(fileContent), (err, data) => {
if (err) r(err);
else s(data);
});
}))
.catch(err => {
console.log(err)
});
};
module.exports.read = (fileId) => {
return new Promise((s, r) => {
mongoose.testingFiles.readById(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
});
};
module.exports.readStream = (fileId) => {
return mongoose.testingFiles.readById(fileId)
};
module.exports.fileInfo = (fileId) => {
return mongoose.testingFiles.findOne({_id: fileId})
.catch(err => {
});
};
module.exports.filesInfo = (fileIds) => {
return mongoose.testingFiles.find({_id: fileIds})
.catch(err => {
});
};
module.exports.remove = (fileId) => {
return new Promise((s, r) => {
mongoose.testingFiles.unlinkById(fileId, (err, data) => {
if (err) r(err);
else s(data);
});
})
.catch(err => {
});
};
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