Skip to content

Commit

Permalink
Merge branch 'master' into oauthSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
dangond-ptc committed Jul 17, 2023
2 parents 34598a5 + cc37474 commit b28f6d4
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 878 deletions.
2 changes: 1 addition & 1 deletion addons/vuforia-spatial-core-addon
19 changes: 16 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@
const fs = require('fs');
const os = require('os');
const path = require('path');

const spatialToolboxPath = path.join(os.homedir(), 'Documents', 'spatialToolbox');
const yargs = require('yargs');

const argv = yargs
.option('spatialToolboxPath', {
description: 'The absolute path to the spatialToolbox directory (default ~/Documents/spatialToolbox)',
type: 'string',
})
.option('udpPort', {
description: 'The port on which udp discovery broadcasts occur (default 52316)',
type: 'number',
})
.help()
.argv;

const spatialToolboxPath = argv.spatialToolboxPath || path.join(os.homedir(), 'Documents', 'spatialToolbox');
const oldRealityObjectsPath = path.join(os.homedir(), 'Documents', 'realityobjects');

// All objects are stored in this folder:
Expand Down Expand Up @@ -38,4 +51,4 @@ if (!fs.existsSync(objectsPath)) {
module.exports.objectsPath = objectsPath;

// this is the port for UDP broadcasting so that the objects find each other
module.exports.beatPort = 52316;
module.exports.beatPort = argv.udpPort || 52316;
51 changes: 41 additions & 10 deletions controllers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ const uploadVideo = function(objectID, videoID, reqForForm, callback) {
}
};

// takes a filename (plus extension) and removes special characters and
// anything non-alphanumeric other than hyphens, underscores, periods
function simplifyFilename(filename) {
// Remove any special characters and replace them with hyphens
filename = filename.replace(/[^\w\s.-]/g, '-');

// Remove any leading or trailing spaces
filename = filename.trim();

// Replace consecutive spaces with a single hyphen
filename = filename.replace(/\s+/g, '-');

// Remove any consecutive hyphens or underscores
filename = filename.replace(/[-_]{2,}/g, '-');

// Remove any non-alphanumeric characters except hyphens, underscores, and periods
filename = filename.replace(/[^\w-.]/g, '');

return filename;
}

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

Expand All @@ -102,15 +123,15 @@ function uploadMediaFile(objectID, req, callback) {
return;
}

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

var form = new formidable.IncomingForm({
let form = new formidable.IncomingForm({
uploadDir: mediaDir,
keepExtensions: true
// accept: 'image/jpeg' // TODO: specify which types of images/videos it accepts?
// accept: 'image/jpeg' // we don't include this anymore, because any filetype can be uploaded
});

console.log('created form');
Expand All @@ -119,29 +140,39 @@ function uploadMediaFile(objectID, req, callback) {
callback(500, err);
});

let mediaUuid = utilities.uuidTime();
let mediaUuid = utilities.uuidTime(); // deprecated
let simplifiedFilename = null;
let newFilepath = null;

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

// rename uploaded file using mediaUuid that is passed back to client
let extension = path.extname(file.path);
newFilepath = form.uploadDir + '/' + mediaUuid + extension;
// simplify the filename so that it only contains alphanumeric, hyphens, underscores, and periods
simplifiedFilename = simplifyFilename(file.originalFilename);
newFilepath = path.join(form.uploadDir, simplifiedFilename);

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

console.log('upload ' + file.path + ' to ' + newFilepath);
file.path = newFilepath;
// old formidable library uses file.path, new version uses file.filepath
if (file.path) {
file.path = newFilepath;
} else if (file.filepath) {
file.filepath = newFilepath;
}
});

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

callback(200, {success: true, mediaUuid: mediaUuid, rawFilepath: newFilepath});
callback(200, {
success: true,
mediaUuid: mediaUuid, // deprecated field
fileName: simplifiedFilename, // the "title" of the file
rawFilepath: newFilepath // this is the actual path to the resource
});
});
}

Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

const fork = require('child_process').fork;
const path = require('path');
const argv = require('process').argv;

const program = path.resolve('server.js');
const parameters = [];
const options = {
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
};

let child = null;

function startNewChild() {
child = fork(program, parameters, options);
child = fork(program, argv, options);
child.on('message', onChildMessage);
child.on('exit', onChildCrash);
}
Expand All @@ -31,7 +31,7 @@ function onChildMessage(message) {

function onChildCrash() {
console.info('server.js has exited unexpectedly, restarting');
child = fork(program, parameters, options);
child = fork(program, argv, options);
}

startNewChild();
4 changes: 3 additions & 1 deletion libraries/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,9 @@ exports.loadHardwareInterface = function loadHardwareInterface(hardwareInterface

} catch (e) {
console.log('Could not load settings.json for: ' + hardwareInterfaceName);
hardwareInterfaces[hardwareInterfaceName] = {};
if (!hardwareInterfaces[hardwareInterfaceName]) {
hardwareInterfaces[hardwareInterfaceName] = {};
}
}

this.read = function (settingsName, defaultvalue) {
Expand Down
Loading

0 comments on commit b28f6d4

Please sign in to comment.