This commit is contained in:
syuilo 2022-02-03 21:38:57 +09:00
parent b7bbe090e9
commit 508af8d458
3 changed files with 38 additions and 21 deletions

View File

@ -11,6 +11,11 @@ import { FILE_TYPE_BROWSERSAFE } from '@/const';
export async function proxyMedia(ctx: Koa.Context) { export async function proxyMedia(ctx: Koa.Context) {
const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url; const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url;
if (typeof url !== 'string') {
ctx.status = 400;
return;
}
// Create temp file // Create temp file
const [path, cleanup] = await createTemp(); const [path, cleanup] = await createTemp();

View File

@ -9,22 +9,34 @@ import { getJson } from '@/misc/fetch';
const logger = new Logger('url-preview'); const logger = new Logger('url-preview');
module.exports = async (ctx: Koa.Context) => { module.exports = async (ctx: Koa.Context) => {
const url = ctx.query.url;
if (typeof url !== 'string') {
ctx.status = 400;
return;
}
const lang = ctx.query.lang;
if (Array.isArray(lang)) {
ctx.status = 400;
return;
}
const meta = await fetchMeta(); const meta = await fetchMeta();
logger.info(meta.summalyProxy logger.info(meta.summalyProxy
? `(Proxy) Getting preview of ${ctx.query.url}@${ctx.query.lang} ...` ? `(Proxy) Getting preview of ${url}@${lang} ...`
: `Getting preview of ${ctx.query.url}@${ctx.query.lang} ...`); : `Getting preview of ${url}@${lang} ...`);
try { try {
const summary = meta.summalyProxy ? await getJson(`${meta.summalyProxy}?${query({ const summary = meta.summalyProxy ? await getJson(`${meta.summalyProxy}?${query({
url: ctx.query.url, url: url,
lang: ctx.query.lang || 'ja-JP', lang: lang ?? 'ja-JP',
})}`) : await summaly(ctx.query.url, { })}`) : await summaly(url, {
followRedirects: false, followRedirects: false,
lang: ctx.query.lang || 'ja-JP', lang: lang ?? 'ja-JP',
}); });
logger.succ(`Got preview of ${ctx.query.url}: ${summary.title}`); logger.succ(`Got preview of ${url}: ${summary.title}`);
summary.icon = wrap(summary.icon); summary.icon = wrap(summary.icon);
summary.thumbnail = wrap(summary.thumbnail); summary.thumbnail = wrap(summary.thumbnail);
@ -33,8 +45,8 @@ module.exports = async (ctx: Koa.Context) => {
ctx.set('Cache-Control', 'max-age=604800, immutable'); ctx.set('Cache-Control', 'max-age=604800, immutable');
ctx.body = summary; ctx.body = summary;
} catch (e) { } catch (err) {
logger.warn(`Failed to get preview of ${ctx.query.url}: ${e}`); logger.warn(`Failed to get preview of ${url}: ${err}`);
ctx.status = 200; ctx.status = 200;
ctx.set('Cache-Control', 'max-age=86400, immutable'); ctx.set('Cache-Control', 'max-age=86400, immutable');
ctx.body = '{}'; ctx.body = '{}';

View File

@ -160,8 +160,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
webpublic: null, webpublic: null,
thumbnail, thumbnail,
}; };
} catch (e) { } catch (err) {
logger.warn(`GenerateVideoThumbnail failed: ${e}`); logger.warn(`GenerateVideoThumbnail failed: ${err}`);
return { return {
webpublic: null, webpublic: null,
thumbnail: null, thumbnail: null,
@ -191,8 +191,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
thumbnail: null, thumbnail: null,
}; };
} }
} catch (e) { } catch (err) {
logger.warn(`sharp failed: ${e}`); logger.warn(`sharp failed: ${err}`);
return { return {
webpublic: null, webpublic: null,
thumbnail: null, thumbnail: null,
@ -215,8 +215,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
} else { } else {
logger.debug(`web image not created (not an required image)`); logger.debug(`web image not created (not an required image)`);
} }
} catch (e) { } catch (err) {
logger.warn(`web image not created (an error occured)`, e); logger.warn(`web image not created (an error occured)`, err as Error);
} }
} else { } else {
logger.info(`web image not created (from remote)`); logger.info(`web image not created (from remote)`);
@ -234,8 +234,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
} else { } else {
logger.debug(`thumbnail not created (not an required file)`); logger.debug(`thumbnail not created (not an required file)`);
} }
} catch (e) { } catch (err) {
logger.warn(`thumbnail not created (an error occured)`, e); logger.warn(`thumbnail not created (an error occured)`, err as Error);
} }
// #endregion thumbnail // #endregion thumbnail
@ -451,9 +451,9 @@ export async function addFile({
file.storedInternal = false; file.storedInternal = false;
file = await DriveFiles.insert(file).then(x => DriveFiles.findOneOrFail(x.identifiers[0])); file = await DriveFiles.insert(file).then(x => DriveFiles.findOneOrFail(x.identifiers[0]));
} catch (e) { } catch (err) {
// duplicate key error (when already registered) // duplicate key error (when already registered)
if (isDuplicateKeyValueError(e)) { if (isDuplicateKeyValueError(err)) {
logger.info(`already registered ${file.uri}`); logger.info(`already registered ${file.uri}`);
file = await DriveFiles.findOne({ file = await DriveFiles.findOne({
@ -461,8 +461,8 @@ export async function addFile({
userId: user ? user.id : null, userId: user ? user.id : null,
}) as DriveFile; }) as DriveFile;
} else { } else {
logger.error(e); logger.error(err as Error);
throw e; throw err;
} }
} }
} else { } else {