Skip to content

Commit

Permalink
added support for assets/tasks file upload. closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Alino committed Aug 9, 2018
1 parent 4df69ce commit 8b48513
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 194 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
typings

assets/assets
assets/deliverables


################################################
Expand Down
68 changes: 39 additions & 29 deletions api/controllers/AssetController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,50 @@
* @description :: Server-side logic for managing assets
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
import express = require('express');
import { Model } from 'sails-typings';
declare var Asset: Model;
declare var Job: Model;

declare var sails;
const path = require('path');

module.exports = {

'uploadFile': (req: express.Request, res: express.Response) => {

// request body should contain:
// isReference
// sourceLanguage
// encoding
// tasks

const id = req.param('id');
console.log(id);

Job.find({ id }).exec((err, theUser) => {
console.log(err);
console.log(theUser);
if (err) return res.send(err);
if (!theUser || theUser.length < 1) return res.send(`job ${id} not found`);
console.log('test');
});


return res.send('body:' + JSON.stringify(req.body));
},

'downloadFile': (req, res) => {
'uploadFile': (req, res) => {
const dirname = path.resolve(sails.config.appPath, 'assets/assets');
req.file('asset').upload({ dirname }, function (err, uploadedFiles) {
if (err) return res.serverError(err);
Asset.create({
sourceLanguage: req.param('sourceLanguage'),
encoding: req.param('encoding'),
jobId: req.param('parentid'),
fileDescriptor: uploadedFiles[0].fd,
fileOriginalName: uploadedFiles[0].filename
}, function (err, result) {
if (err) return res.serverError(err);
return res.json(result);
});
});
},

'downloadFile': (req, res) => {
Asset.findOne(req.param('id')).exec(function (err, asset) {
if (err) return res.serverError(err);
if (!asset) return res.notFound();
if (!asset.fileDescriptor) return res.notFound();

const SkipperDisk = require('skipper-disk');
const fileAdapter = SkipperDisk();

// set the filename to the same file as the user uploaded
res.set("Content-disposition", "attachment; filename=" + asset.fileOriginalName + "");

// Stream the file down
fileAdapter.read(asset.fileDescriptor)
.on('error', function (err) {
return res.serverError(err);
})
.pipe(res);
});
}

}

};

64 changes: 37 additions & 27 deletions api/controllers/TaskController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,50 @@
* @description :: Server-side logic for managing tasks
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
import express = require('express');
import { Model } from 'sails-typings';
declare var Task: Model;
declare var Job: Model;
declare var sails;
const path = require('path');



module.exports = {

'uploadFile': (req: express.Request, res: express.Response) => {

// request body should contain:
// isReference
// sourceLanguage
// encoding
// tasks

const id = req.param('id');
console.log(id);

Job.find({ id }).exec((err, theUser) => {
console.log(err);
console.log(theUser);
if (err) return res.send(err);
if (!theUser || theUser.length < 1) return res.send(`job ${id} not found`);
console.log('test');
'uploadFile': (req, res) => {
const dirname = path.resolve(sails.config.appPath, 'assets/deliverables');
req.file('deliverable').upload({ dirname }, function (err, uploadedFiles) {
if (err) return res.serverError(err);
Task.update(req.param('id'), {
progress: 'finished',
fileDescriptor: uploadedFiles[0].fd,
fileOriginalName: uploadedFiles[0].filename
}, function (err, result) {
if (err) return res.serverError(err);
return res.json(result);
});
});


return res.send('body:' + JSON.stringify(req.body));
},

'downloadFile': (req, res) => {

}
},

'downloadFile': (req, res) => {
Task.findOne(req.param('id')).exec(function (err, task) {
if (err) return res.serverError(err);
if (!task) return res.notFound();
if (!task.fileDescriptor) return res.notFound();

const SkipperDisk = require('skipper-disk');
const fileAdapter = SkipperDisk();

// set the filename to the same file as the user uploaded
res.set("Content-disposition", "attachment; filename=" + task.fileOriginalName + "");

// Stream the file down
fileAdapter.read(task.fileDescriptor)
.on('error', function (err) {
return res.serverError(err);
})
.pipe(res);
});
}

};

11 changes: 8 additions & 3 deletions api/models/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ module.exports = {
description: '(auto-generated)'
},

file: {
type: 'binary',
description: 'an actual file (asset)'
fileDescriptor: {
type: 'string',
description: 'unique name of the file. (auto-generated)'
},

fileOriginalName: {
type: 'string',
description: 'original name of the file as uploaded. (auto-filled)'
},

isReference: {
Expand Down
11 changes: 8 additions & 3 deletions api/models/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ finished - the work on this Task is done and deliverableLocation is filled with
example: 'symfonie.com/43920149320'
},

file: {
type: 'binary',
description: 'an actual file (deliverable)'
fileDescriptor: {
type: 'string',
description: 'unique name of the file. (auto-generated)'
},

fileOriginalName: {
type: 'string',
description: 'original name of the file as uploaded. (auto-filled)'
},

jobId: {
Expand Down
Loading

0 comments on commit 8b48513

Please sign in to comment.