Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload file from link #12

Open
kudlav opened this issue Jul 2, 2019 · 2 comments
Open

Upload file from link #12

kudlav opened this issue Jul 2, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@kudlav
Copy link
Owner

kudlav commented Jul 2, 2019

No description provided.

@kudlav kudlav added the enhancement New feature or request label Jul 2, 2019
@kudlav kudlav changed the title Upload file from link or use it from shared library Upload file from link Jul 9, 2020
@pegasus1982
Copy link

@kudlav I tried to implement this feature.

I added this code section to controllers/apiController.js.

const download = require('download');
...
exports.projectRemoteFilePOST = (req, res, next) => {
	let url = req.body.url;
	let filename = url.split('/');
	filename = filename[filename.length - 1];
	const fileID = nanoid();
	const extension = path.extname(filename);
	let filepath = path.join(config.projectPath, req.params.projectID, fileID);
	if (extension.length > 1) filepath += extension;

	log.info(`Save of "${filename}" from "${url}" started`);

	console.log('save remote file ', url);

	// // Create a write stream of the new file
	let mimeType = 'video/mp4';

	const fstream = fs.createWriteStream(filepath);
	fstream.on('finish', () => {
		log.info(`Save of "${filename}" from "${url}" finished`);
		console.log('download finished');
		fileManager.getDuration(filepath, mimeType).then(
			length => {
				projectManager.load(req.params.projectID, 'w').then(
					([document, , release]) => {
						const node = document.createElement('producer');
						node.id = 'producer' + fileID;
						node.innerHTML = `<property name="resource">${path.resolve(filepath)}</property>`;
						node.innerHTML += `<property name="musecut:mime_type">${mimeType}</property>`;
						node.innerHTML += `<property name="musecut:name">${filename}</property>`;
						if (length !== null) {
							if (timeManager.isValidDuration(length))
								node.innerHTML += `<property name="length">${length}</property>`;
							else {
								length = null;
								log.fatal(`Unable to get duration of ${mimeType}: ${filepath}`);
							}
						}

						const root = document.getElementsByTagName('mlt').item(0);
						root.prepend(node);
						projectManager.save(req.params.projectID, root.outerHTML, release).then(
							() => res.json({
								msg: `Upload of "${filename}" OK`,
								resource_id: fileID,
								resource_mime: mimeType,
								length: length,
							}),
							err => next(err)
						);
					},
					err => fileErr(err, res)
				);
			}
		);
	})
	fstream.on('error', () => errorResponse(error.projectNotFound404, res));

	try {
		download(url).pipe(fstream);
	} catch (_) {
		console.log('download failed')
	}
};

And I added route
router.post('/api/project/:projectID/url', apiController.projectRemoteFilePOST);

I can't confirm that this way is absolutely right.
Anyway, it works properly.

@kudlav
Copy link
Owner Author

kudlav commented Jul 31, 2020

Hi @pegasus1982, thanks for the code. It looks simply, with some changes I will implement it. I would rather use "node-fetch" library as I already have this library as dev-dependency and it allow me to access Content-Type header containing mime-type (you have mime-type hardcoded to 'video/mp4').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants