2022-02-27 03:07:39 +01:00
|
|
|
import { URL } from 'node:url';
|
|
|
|
import { addFile } from './add-file.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { driveLogger } from './logger.js';
|
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
|
|
|
import { downloadUrl } from '@/misc/download-url.js';
|
|
|
|
import { DriveFolder } from '@/models/entities/drive-folder.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { DriveFiles } from '@/models/index.js';
|
2018-05-25 13:19:14 +02:00
|
|
|
|
2019-02-03 08:45:13 +01:00
|
|
|
const logger = driveLogger.createSubLogger('downloader');
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2022-01-23 14:52:35 +01:00
|
|
|
type Args = {
|
|
|
|
url: string;
|
|
|
|
user: { id: User['id']; host: User['host'] } | null;
|
|
|
|
folderId?: DriveFolder['id'] | null;
|
|
|
|
uri?: string | null;
|
|
|
|
sensitive?: boolean;
|
|
|
|
force?: boolean;
|
|
|
|
isLink?: boolean;
|
|
|
|
comment?: string | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function uploadFromUrl({
|
|
|
|
url,
|
|
|
|
user,
|
|
|
|
folderId = null,
|
|
|
|
uri = null,
|
2018-11-07 04:12:43 +01:00
|
|
|
sensitive = false,
|
2018-11-07 11:43:21 +01:00
|
|
|
force = false,
|
2022-01-23 14:52:35 +01:00
|
|
|
isLink = false,
|
2020-10-17 13:12:00 +02:00
|
|
|
comment = null
|
2022-01-23 14:52:35 +01:00
|
|
|
}: Args): Promise<DriveFile> {
|
2019-04-12 18:43:22 +02:00
|
|
|
let name = new URL(url).pathname.split('/').pop() || null;
|
|
|
|
if (name == null || !DriveFiles.validateFileName(name)) {
|
2018-03-27 09:51:12 +02:00
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2021-05-28 02:38:09 +02: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 09:51:12 +02:00
|
|
|
// Create temp file
|
2019-03-20 20:50:44 +01:00
|
|
|
const [path, cleanup] = await createTemp();
|
2018-03-27 09:51:12 +02:00
|
|
|
|
|
|
|
// write content at URL to temp file
|
2019-03-20 20:50:44 +01:00
|
|
|
await downloadUrl(url, path);
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
let driveFile: DriveFile;
|
2018-04-09 21:11:52 +02:00
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
2022-01-23 14:52:35 +01:00
|
|
|
driveFile = await addFile({ user, path, name, comment, folderId, force, isLink, url, uri, sensitive });
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.succ(`Got: ${driveFile.id}`);
|
2018-04-09 21:11:52 +02:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
2019-03-03 00:00:39 +01:00
|
|
|
logger.error(`Failed to create drive file: ${e}`, {
|
|
|
|
url: url,
|
2021-12-09 15:58:30 +01:00
|
|
|
e: e,
|
2019-03-03 00:00:39 +01:00
|
|
|
});
|
2018-04-09 21:11:52 +02:00
|
|
|
}
|
2018-04-05 11:08:51 +02:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
// clean-up
|
2018-04-09 21:02:25 +02:00
|
|
|
cleanup();
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2018-04-09 21:11:52 +02:00
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
} else {
|
2019-04-12 18:43:22 +02:00
|
|
|
return driveFile!;
|
2018-04-09 21:11:52 +02:00
|
|
|
}
|
2022-01-23 14:52:35 +01:00
|
|
|
}
|