2021-03-24 11:05:37 +09:00
|
|
|
import { URL } from 'url';
|
2022-01-20 02:43:13 +09:00
|
|
|
import { addFile } from './add-file';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { driveLogger } from './logger';
|
|
|
|
import { createTemp } from '@/misc/create-temp';
|
|
|
|
import { downloadUrl } from '@/misc/download-url';
|
|
|
|
import { DriveFolder } from '@/models/entities/drive-folder';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file';
|
|
|
|
import { DriveFiles } from '@/models/index';
|
2018-05-25 20:19:14 +09:00
|
|
|
|
2019-02-03 16:45:13 +09:00
|
|
|
const logger = driveLogger.createSubLogger('downloader');
|
2018-03-27 16:51:12 +09:00
|
|
|
|
2018-11-07 12:12:43 +09:00
|
|
|
export default async (
|
|
|
|
url: string,
|
2021-03-24 11:05:37 +09:00
|
|
|
user: { id: User['id']; host: User['host'] } | null,
|
2019-04-13 01:43:22 +09:00
|
|
|
folderId: DriveFolder['id'] | null = null,
|
|
|
|
uri: string | null = null,
|
2018-11-07 12:12:43 +09:00
|
|
|
sensitive = false,
|
2018-11-07 19:43:21 +09:00
|
|
|
force = false,
|
2020-10-17 20:12:00 +09:00
|
|
|
link = false,
|
|
|
|
comment = null
|
2019-04-07 21:50:36 +09:00
|
|
|
): Promise<DriveFile> => {
|
2019-04-13 01:43:22 +09:00
|
|
|
let name = new URL(url).pathname.split('/').pop() || null;
|
|
|
|
if (name == null || !DriveFiles.validateFileName(name)) {
|
2018-03-27 16:51:12 +09:00
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2021-05-28 09:38:09 +09:00
|
|
|
// If the comment is same as the name, skip comment
|
|
|
|
// (image.name is passed in when receiving attachment)
|
|
|
|
if (comment !== null && name == comment) {
|
|
|
|
comment = null;
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:51:12 +09:00
|
|
|
// Create temp file
|
2019-03-21 04:50:44 +09:00
|
|
|
const [path, cleanup] = await createTemp();
|
2018-03-27 16:51:12 +09:00
|
|
|
|
|
|
|
// write content at URL to temp file
|
2019-03-21 04:50:44 +09:00
|
|
|
await downloadUrl(url, path);
|
2018-03-27 16:51:12 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
let driveFile: DriveFile;
|
2018-04-10 04:11:52 +09:00
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
2022-01-20 02:43:13 +09:00
|
|
|
driveFile = await addFile(user, path, name, comment, folderId, force, link, url, uri, sensitive);
|
2019-04-07 21:50:36 +09:00
|
|
|
logger.succ(`Got: ${driveFile.id}`);
|
2018-04-10 04:11:52 +09:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
2019-03-03 08:00:39 +09:00
|
|
|
logger.error(`Failed to create drive file: ${e}`, {
|
|
|
|
url: url,
|
2021-12-09 23:58:30 +09:00
|
|
|
e: e,
|
2019-03-03 08:00:39 +09:00
|
|
|
});
|
2018-04-10 04:11:52 +09:00
|
|
|
}
|
2018-04-05 18:08:51 +09:00
|
|
|
|
2018-03-27 16:51:12 +09:00
|
|
|
// clean-up
|
2018-04-10 04:02:25 +09:00
|
|
|
cleanup();
|
2018-03-27 16:51:12 +09:00
|
|
|
|
2018-04-10 04:11:52 +09:00
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
} else {
|
2019-04-13 01:43:22 +09:00
|
|
|
return driveFile!;
|
2018-04-10 04:11:52 +09:00
|
|
|
}
|
2018-03-27 16:51:12 +09:00
|
|
|
};
|