Skip to content

Commit

Permalink
adds POST /uploadMediaFile and GET /mediaFile for a given object to s…
Browse files Browse the repository at this point in the history
…tore videos and photos in its spatialToolbox/objectName/mediaFiles directory
  • Loading branch information
benptc committed Mar 19, 2021
1 parent 548c917 commit 322dcf8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
53 changes: 53 additions & 0 deletions controllers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,58 @@ const uploadVideo = function(objectID, videoID, reqForForm, callback) {
}
};

function uploadMediaFile(objectID, req, callback) {
console.log('received media file for', objectID);

let object = utilities.getObject(objects, objectID);
if (!object) {
callback(404, 'object ' + objectID + ' not found');
return;
}

var mediaDir = objectsPath + '/' + object.name + '/' + identityFolderName + '/mediaFiles';
if (!fs.existsSync(mediaDir)) {
fs.mkdirSync(mediaDir);
}

var form = new formidable.IncomingForm({
uploadDir: mediaDir,
keepExtensions: true
// accept: 'image/jpeg' // TODO: specify which types of images/videos it accepts?
});

console.log('created form');

form.on('error', function (err) {
callback(500, err);
});

let mediaUuid = utilities.uuidTime();

var rawFilepath = form.uploadDir + '/' + mediaUuid + '.jpg';

if (fs.existsSync(rawFilepath)) {
console.log('deleted old raw file');
fs.unlinkSync(rawFilepath);
}

form.on('fileBegin', function (name, file) {
console.log('fileBegin loading', name, file);

if (file.type.match('video.*')) {
rawFilepath = rawFilepath.replace('.jpg', '.mov');
}
console.log('upload ' + file.path + ' to ' + rawFilepath);
file.path = rawFilepath;
});

form.parse(req, function (err, fields) {
console.log('successfully uploaded image', err, fields);

callback(200, {success: true, mediaUuid: mediaUuid, rawFilepath: rawFilepath});
});
}

const saveCommit = function(objectID, callback) {
if (globalVariables.isMobile) {
callback(500, 'saveCommit unavailable on mobile');
Expand Down Expand Up @@ -309,6 +361,7 @@ const setup = function (objects_, globalVariables_, hardwareAPI_, objectsPath_,

module.exports = {
uploadVideo: uploadVideo,
uploadMediaFile: uploadMediaFile,
saveCommit: saveCommit,
resetToLastCommit: resetToLastCommit,
setMatrix: setMatrix,
Expand Down
5 changes: 4 additions & 1 deletion libraries/objectDefaultFiles/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
object: '',
publicData: {},
modelViewMatrix: [],
serverIp: '127.0.0,1',
serverIp: '127.0.0.1',
serverPort: '8080',
matrices: {
modelView: [],
projection: [],
Expand Down Expand Up @@ -107,6 +108,8 @@
let defaultPort = '8080';
if (object.hasOwnProperty('port')) defaultPort = object.port;

spatialObject.serverPort = defaultPort;

var url = 'http://' + object.ip + ':' + defaultPort;
spatialObject.socketIoUrl = url;
script.src = url + '/socket.io/socket.io.js';
Expand Down
13 changes: 13 additions & 0 deletions routers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ router.post('/:objectName/video/:videoName', function (req, res) {
}
});
});
router.post('/:objectName/uploadMediaFile', function (req, res) {
if (!utilities.isValidId(req.params.objectName)) {
res.status(400).send('Invalid object or frame name. Must be alphanumeric.');
return;
}
objectController.uploadMediaFile(req.params.objectName, req, function (statusCode, responseContents) {
if (statusCode === 500) {
res.status(statusCode).send(responseContents);
} else {
res.status(statusCode).json(responseContents).end();
}
});
});
// object git interfaces
router.post('/:objectName/saveCommit', function (req, res) {
if (!utilities.isValidId(req.params.objectName)) {
Expand Down
18 changes: 18 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,24 @@ function objectWebServer() {
res.sendFile(fileName);
});

webServer.use('/mediaFile', function (req, res) {
var urlArray = req.originalUrl.split('/');

var objectId = urlArray[2];
if (!getObject(objectId)) {
res.status(404).send('object ' + objectId + ' not found');
return;
}

var objectName = getObject(objectId).name;
var fileName = objectsPath + '/' + objectName + '/' + identityFolderName + '/mediaFiles/' + urlArray[3];
if (!fs.existsSync(fileName)) {
res.sendFile(__dirname + '/libraries/emptyLogicIcon.png'); // default to blank image if not found
return;
}
res.sendFile(fileName);
});

webServer.use('/obj', function (req, res, next) {

var urlArray = req.originalUrl.split('/');
Expand Down

0 comments on commit 322dcf8

Please sign in to comment.