2018-04-29 20:33:27 +00:00
|
|
|
const map = require('lodash/map');
|
|
|
|
const filter = require('lodash/filter');
|
2018-04-26 12:08:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Upload
|
|
|
|
{
|
|
|
|
constructor(values)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.id = values.id || values._id || null;
|
|
|
|
self.userId = values.userId || null;
|
2018-04-28 07:24:53 +00:00
|
|
|
self.code = values.code || null;
|
2018-04-26 12:08:08 +00:00
|
|
|
self.created = values.created || new Date();
|
|
|
|
self.expiration = values.expiration || null;
|
|
|
|
self.files = values.files || [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
class UploadRepository
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
constructor(store)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
self.store = store;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
init()
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
// Initialize database if empty
|
|
|
|
self.store.count({}, (err, count) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
self.addUser('admin', null, 'test', null);
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
insert(userId, code, files, expiration)
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
var upload = {
|
|
|
|
created: new Date(),
|
2018-04-26 12:08:08 +00:00
|
|
|
userId: userId,
|
2018-04-27 15:13:18 +00:00
|
|
|
code: code,
|
2018-03-21 15:51:46 +00:00
|
|
|
expiration: expiration,
|
2018-04-29 20:33:27 +00:00
|
|
|
files: map(filter(files,
|
|
|
|
(file) => file.hasOwnProperty('id') && file.hasOwnProperty('name')),
|
|
|
|
(file) => { return { id: file.id, name: file.name, size: file.size } })
|
2018-03-21 15:51:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (upload.files.length)
|
|
|
|
{
|
|
|
|
self.store.insert(upload, (err, dbUpload) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(dbUpload._id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-04-26 12:08:08 +00:00
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
list(userId)
|
2018-04-26 12:08:08 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.find(userId != null ? { userId: userId } : {}, (err, docs) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(docs.map((dbUpload) =>
|
|
|
|
{
|
|
|
|
return new Upload(dbUpload);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-04-28 07:24:53 +00:00
|
|
|
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
get(uploadId)
|
2018-04-28 07:24:53 +00:00
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.findOne({ _id: uploadId }, (err, doc) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(doc !== null ? new Upload(doc) : null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
delete(uploadId)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.remove({ _id: uploadId }, (err, numRemoved) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fileExists(fileId)
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
self.store.findOne({ 'files.id': fileId }, (err, doc) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(doc !== null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-03-21 15:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
module.exports = {
|
|
|
|
UploadRepository
|
|
|
|
}
|