Git Product home page Git Product logo

Comments (2)

pegasus1982 avatar pegasus1982 commented on August 15, 2024

@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.

from videoeditor.

kudlav avatar kudlav commented on August 15, 2024

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').

from videoeditor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.