node-test-bed-adapter
Version:
An adapter to connect a node.js application to the Test-bed's Common Information Space or Common Simulation Space.
103 lines • 3.73 kB
JavaScript
import { AdapterLogger } from '../index.mjs';
import * as fs from 'fs';
import * as path from 'path';
import FormData from 'form-data';
import { default as axios } from 'axios';
import { bufferToStream } from '../utils/index.mjs';
/**
* Service to help the user upload large files to the Test-bed.
* Files can be upload to the public folder, or obscure (semi-private).
* If requested, a message can be sent to the Test-bed to inform others.
*/
export class LargeFileUploadService {
restUri;
log = AdapterLogger.instance;
constructor(options) {
if (!options.largeFileService) {
return;
}
const addHttp = (s) => (s.startsWith('http') ? s : `http://${s}`);
const removeTrailingSlash = (s) => s.endsWith('/') ? s.substring(0, s.length - 1) : s;
this.restUri = removeTrailingSlash(addHttp(options.largeFileService));
this.log.info('URL: ' + this.restUri);
}
/** Is the service enabled */
get enabled() {
return this.restUri ? true : false;
}
/**
* Upload the file to the LargeFileService.
*
* @param file File path or an object containing a buffer and name to upload
* @param isPrivate When true, the file will not show up in the public listing
* @param cb Callback functions, invoked when the upload is done (or errors).
*/
upload(file, isPrivate = false, cb) {
if (!this.enabled) {
return;
}
if (typeof file !== 'string') {
this.uploadBuffer(file, isPrivate, cb);
}
else {
fs.exists(file, (existing) => {
if (existing) {
this.uploadFile(file, isPrivate, cb);
}
else {
const filePath = path.resolve(process.cwd(), file);
fs.exists(filePath, (exists) => {
if (exists) {
this.uploadFile(file, isPrivate, cb);
}
else if (cb) {
cb(new Error(`Error: file ${filePath} does not exist`));
}
});
}
});
}
}
/** Actually upload the file to the large-file-service */
uploadFile(file, isPrivate, cb) {
if (!this.restUri) {
return;
}
const form = new FormData();
form.append('uploadFile', fs.createReadStream(file), path.basename(file));
form.append('private', isPrivate.toString());
this.uploadForm(form, cb);
}
/**
* Upload the buffer to the LargeFileService.
*
* @param file File to upload or an object containing a Buffer and name
* @param isPrivate When true, the file will not show up in the public listing
* @param cb Callback functions, invoked when the upload is done
*/
uploadBuffer(namedBuffer, isPrivate = false, cb) {
if (!this.restUri) {
return;
}
const { buffer, name } = namedBuffer;
const stream = bufferToStream(buffer);
const form = new FormData();
form.append('uploadFile', stream, name);
form.append('private', isPrivate.toString());
this.uploadForm(form, cb);
}
/** Upload the form to the service */
uploadForm(form, cb) {
axios
.post(`${this.restUri}/upload`, form, {
headers: form.getHeaders(),
})
.then((res) => {
cb && res.data && cb(undefined, res.data.FileURL);
})
.catch((err) => {
cb ? cb(err) : this.log.error(err);
});
}
}
//# sourceMappingURL=large-file-upload-service.mjs.map