More changes for db migration

This commit is contained in:
Jonathan Cremin 2016-08-07 14:38:05 +01:00
parent de0284e48a
commit 889dc02945
33 changed files with 740 additions and 100 deletions

View file

@ -22,8 +22,8 @@ export function formatSize(size) {
export function formatFile(file) {
const formattedFile = {
added: moment.unix(file.createdAt).format(),
readableAdded: formatDate(file.createdAt),
added: moment.unix(file.createdAt / 1000).format(),
readableAdded: formatDate(file.createdAt / 1000),
downloads: file.downloads !== undefined ? file.downloads : 0,
href: `${baseURL}/${file.id}`,
id: file.id,
@ -32,7 +32,7 @@ export function formatFile(file) {
readableSize: formatSize(file.size),
type: sniff(file.name),
trashed: (file.status === 'trashed'),
status: file.status,
status: file.processed === true ? 'active' : 'uploading',
};
if (file.width) {

View file

@ -71,7 +71,7 @@ export default function* (file) {
}
const result = yield virustotal.getFileReport(file.md5);
return {
positives: result.positives,
positive: result.positives >= 5,
result,
};
}

View file

@ -4,7 +4,7 @@ import debugname from 'debug';
const debug = debugname('hostr:mongo');
/* eslint no-param-reassign: ["error", { "props": false }] */
const configuredClient = new Promise((resolve, reject) => {
export const mongo = new Promise((resolve, reject) => {
debug('Connecting to Mongodb');
return MongoClient.connect(process.env.MONGO_URL).then((client) => {
debug('Successfully connected to Mongodb');
@ -25,10 +25,10 @@ const configuredClient = new Promise((resolve, reject) => {
debug(e);
});
export default function mongo() {
export default function () {
return function* dbMiddleware(next) {
try {
this.db = yield configuredClient;
this.db = yield mongo;
} catch (e) {
debug(e);
}

View file

@ -55,17 +55,17 @@ function scale(path, type, size) {
});
}
export default function resize(path, type, currentSize, dim) {
export default function resize(path, type, currentSize, newSize) {
debug('Resizing');
const ratio = 970 / currentSize.width;
debug(dim.width, ratio);
if (dim.width <= 150) {
debug(newSize.width, ratio);
if (newSize.width <= 150) {
debug('Cover');
return cover(path, type, dim);
} else if (dim.width >= 970 && ratio < 1) {
return cover(path, type, newSize);
} else if (newSize.width >= 970 && ratio < 1) {
debug('Scale');
dim.height = currentSize.height * ratio; // eslint-disable-line no-param-reassign
return scale(path, type, dim);
newSize.height = currentSize.height * ratio; // eslint-disable-line no-param-reassign
return scale(path, type, newSize);
}
debug('Copy');
return fs.readFile(path);

View file

@ -135,12 +135,13 @@ export default class Uploader {
*checkLimit() {
const count = yield models.file.count({
userId: this.context.user.id,
createdAt: {
$gt: Math.ceil(Date.now() / 1000) - 86400,
where: {
userId: this.context.user.id,
createdAt: {
$gt: Date.now() - 86400000,
},
},
});
debug(count);
const userLimit = this.context.user.daily_upload_allowance;
const underLimit = (count < userLimit || userLimit === 'unlimited');
if (!underLimit) {
@ -158,6 +159,7 @@ export default class Uploader {
*finalise() {
this.file.size = this.receivedSize;
this.file.status = 'active';
this.file.processed = 'true';
yield this.file.save();
}