summaryrefslogtreecommitdiff
path: root/src/server/api/common/drive/upload_from_url.ts
blob: 5dd96959367f6c9e0627736e8b57c464f5e2e966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as URL from 'url';
import { IDriveFile, validateFileName } from '../../models/drive-file';
import create from './add-file';
import * as debug from 'debug';
import * as tmp from 'tmp';
import * as fs from 'fs';
import * as request from 'request';

const log = debug('misskey:common:drive:upload_from_url');

export default async (url, user, folderId = null): Promise<IDriveFile> => {
	let name = URL.parse(url).pathname.split('/').pop();
	if (!validateFileName(name)) {
		name = null;
	}

	// Create temp file
	const path = await new Promise((res: (string) => void, rej) => {
		tmp.file((e, path) => {
			if (e) return rej(e);
			res(path);
		});
	});

	// write content at URL to temp file
	await new Promise((res, rej) => {
		const writable = fs.createWriteStream(path);
		request(url)
			.on('error', rej)
			.on('end', () => {
				writable.close();
				res(path);
			})
			.pipe(writable)
			.on('error', rej);
	});

	const driveFile = await create(user, path, name, null, folderId);

	// clean-up
	fs.unlink(path, (e) => {
		if (e) log(e.stack);
	});

	return driveFile;
};