Skip to content

Commit 02c2fc7

Browse files
author
Samy Laumonier
committed
mise en place node-celery
1 parent 522451d commit 02c2fc7

File tree

7 files changed

+55
-18
lines changed

7 files changed

+55
-18
lines changed
File renamed without changes.

meteor/client/pages/profile/file/actions.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
Template.profileFileActions.events({
22
'click .convert': function (event, template) {
3-
const button = $(event.target);
4-
5-
if (button.is(':disabled')) {
6-
return false;
7-
}
8-
9-
button.prop('disabled', true);
10-
113
const modal = ReactiveModal.initDialog({
124
template: Template.profileAskFormat,
135
title: 'Convert your file',
@@ -42,6 +34,10 @@ Template.profileFileActions.events({
4234
});
4335
});
4436

37+
modal.buttons.convert.on('cancel', () => {
38+
button.prop('disabled', false);
39+
});
40+
4541
modal.show();
4642
},
4743
'click .delete': function (event, template) {
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#upload .progress-label {
2+
overflow: hidden;
3+
text-overflow: ellipsis;
4+
white-space: nowrap;
5+
}

meteor/lib/tabulars/file.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ new Tabular.Table({
66
collection: Collection.Files,
77
columns: [
88
{ data: 'originalName', title: 'Name' },
9-
{ data: 'type', title: 'Type' },
9+
{ data: 'ext', title: 'Format' },
1010
{ data: 'numeralSize()', title: 'Size' },
1111
{ data: 'state()', title: 'Status' },
1212
{
@@ -17,5 +17,5 @@ new Tabular.Table({
1717
title: 'Actions'
1818
}
1919
],
20-
extraFields: ['size', 'status', 'ext', 'isAudio', 'isVideo', 'price']
20+
extraFields: ['size', 'status', 'isAudio', 'isVideo', 'price']
2121
});

meteor/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"download-file": "^0.1.5",
1010
"fibers": "^1.0.13",
1111
"fs": "0.0.2",
12+
"hiredis": "^0.4.1",
1213
"mime": "^1.3.4",
1314
"node-celery": "^0.2.6"
1415
}

meteor/server/file/hook.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ Collection.Files.before.insert(function (userId, doc) {
1212

1313
doc.createdBy = user._id;
1414
doc.status = 'new';
15-
doc.ext = _.last(doc.type.split('/'));
15+
doc.ext = _.last(doc.originalName.split('.'));
1616

1717
doc.isAudio = _.contains(Meteor.settings.public.formats.audio, doc.ext);
1818
doc.isVideo = _.contains(Meteor.settings.public.formats.video, doc.ext);
1919

20-
const price = doc.size / 1048576;
20+
const price = doc.size / 1048576 * 0.01;
2121
doc.price = price.toFixed(2);
2222
});
2323

meteor/server/file/method.js

+41-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import url from 'url';
33
import mime from 'mime';
44
import download from 'download-file';
55
import Future from 'fibers/future';
6+
import celery from 'node-celery';
67

78
Meteor.methods({
89
uploadFromUrl: function (link) {
@@ -83,18 +84,52 @@ Meteor.methods({
8384
}
8485
});
8586

86-
// TODO: call celery
87+
const client = celery.createClient({
88+
CELERY_BROKER_URL: Meteor.settings.broker,
89+
CELERY_RESULT_BACKEND: 'amqp'
90+
});
91+
92+
client.on('error', err => {
93+
console.log(err);
94+
});
95+
96+
client.on('connect', Meteor.bindEnvironment(() => {
97+
const inputPath = `/data${file.path}`;
98+
99+
client.call('core.process_file', [file.isVideo, inputPath, outputFormat], Meteor.bindEnvironment(outputPath => {
100+
client.end();
101+
102+
if (fs.existsSync(inputPath)) {
103+
fs.unlinkSync(inputPath);
104+
}
105+
106+
if (fs.existsSync(outputPath)) {
107+
const newPath = outputPath.replace('/tmp', '');
108+
fs.renameSync(outputPath, newPath);
109+
}
110+
111+
console.log('outputPath:', outputPath);
112+
113+
Collection.Files.update(fileId, {
114+
$set: {
115+
status: 'converted'
116+
}
117+
});
118+
119+
Meteor.users.update(Meteor.userId(), {
120+
$inc: {
121+
'profile.diskUsage': -size
122+
}
123+
});
124+
}));
125+
}));
87126
},
88127
deleteFile: (fileId) => {
89128
const file = getFile(fileId);
90129
const filePath = Meteor.settings.data + file.path;
91130

92131
if (fs.existsSync(filePath)) {
93-
fs.unlink(filePath, error => {
94-
if (error) {
95-
throw new Meteor.Error(error.toString());
96-
}
97-
});
132+
fs.unlinkSync(filePath);
98133
}
99134

100135
const size = file.size;

0 commit comments

Comments
 (0)