mirror of
https://github.com/paricafe/misskey.git
synced 2024-11-29 17:06:43 -06:00
b9cb6d1c10
将来ESMに移行しやすいように Related: #7658 なんかmochaが起動しなくなってるけど理由不明 すぐ直したい
62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import { URL } from 'url';
|
|
import create 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';
|
|
|
|
const logger = driveLogger.createSubLogger('downloader');
|
|
|
|
export default async (
|
|
url: string,
|
|
user: { id: User['id']; host: User['host'] } | null,
|
|
folderId: DriveFolder['id'] | null = null,
|
|
uri: string | null = null,
|
|
sensitive = false,
|
|
force = false,
|
|
link = false,
|
|
comment = null
|
|
): Promise<DriveFile> => {
|
|
let name = new URL(url).pathname.split('/').pop() || null;
|
|
if (name == null || !DriveFiles.validateFileName(name)) {
|
|
name = null;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Create temp file
|
|
const [path, cleanup] = await createTemp();
|
|
|
|
// write content at URL to temp file
|
|
await downloadUrl(url, path);
|
|
|
|
let driveFile: DriveFile;
|
|
let error;
|
|
|
|
try {
|
|
driveFile = await create(user, path, name, comment, folderId, force, link, url, uri, sensitive);
|
|
logger.succ(`Got: ${driveFile.id}`);
|
|
} catch (e) {
|
|
error = e;
|
|
logger.error(`Failed to create drive file: ${e}`, {
|
|
url: url,
|
|
e: e
|
|
});
|
|
}
|
|
|
|
// clean-up
|
|
cleanup();
|
|
|
|
if (error) {
|
|
throw error;
|
|
} else {
|
|
return driveFile!;
|
|
}
|
|
};
|