mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-10 02:23:09 +02:00
refactor
This commit is contained in:
parent
b7bbe090e9
commit
508af8d458
3 changed files with 38 additions and 21 deletions
|
@ -11,6 +11,11 @@ import { FILE_TYPE_BROWSERSAFE } from '@/const';
|
|||
export async function proxyMedia(ctx: Koa.Context) {
|
||||
const url = 'url' in ctx.query ? ctx.query.url : 'https://' + ctx.params.url;
|
||||
|
||||
if (typeof url !== 'string') {
|
||||
ctx.status = 400;
|
||||
return;
|
||||
}
|
||||
|
||||
// Create temp file
|
||||
const [path, cleanup] = await createTemp();
|
||||
|
||||
|
|
|
@ -9,22 +9,34 @@ import { getJson } from '@/misc/fetch';
|
|||
const logger = new Logger('url-preview');
|
||||
|
||||
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();
|
||||
|
||||
logger.info(meta.summalyProxy
|
||||
? `(Proxy) Getting preview of ${ctx.query.url}@${ctx.query.lang} ...`
|
||||
: `Getting preview of ${ctx.query.url}@${ctx.query.lang} ...`);
|
||||
? `(Proxy) Getting preview of ${url}@${lang} ...`
|
||||
: `Getting preview of ${url}@${lang} ...`);
|
||||
|
||||
try {
|
||||
const summary = meta.summalyProxy ? await getJson(`${meta.summalyProxy}?${query({
|
||||
url: ctx.query.url,
|
||||
lang: ctx.query.lang || 'ja-JP',
|
||||
})}`) : await summaly(ctx.query.url, {
|
||||
url: url,
|
||||
lang: lang ?? 'ja-JP',
|
||||
})}`) : await summaly(url, {
|
||||
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.thumbnail = wrap(summary.thumbnail);
|
||||
|
@ -33,8 +45,8 @@ module.exports = async (ctx: Koa.Context) => {
|
|||
ctx.set('Cache-Control', 'max-age=604800, immutable');
|
||||
|
||||
ctx.body = summary;
|
||||
} catch (e) {
|
||||
logger.warn(`Failed to get preview of ${ctx.query.url}: ${e}`);
|
||||
} catch (err) {
|
||||
logger.warn(`Failed to get preview of ${url}: ${err}`);
|
||||
ctx.status = 200;
|
||||
ctx.set('Cache-Control', 'max-age=86400, immutable');
|
||||
ctx.body = '{}';
|
||||
|
|
|
@ -160,8 +160,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
|
|||
webpublic: null,
|
||||
thumbnail,
|
||||
};
|
||||
} catch (e) {
|
||||
logger.warn(`GenerateVideoThumbnail failed: ${e}`);
|
||||
} catch (err) {
|
||||
logger.warn(`GenerateVideoThumbnail failed: ${err}`);
|
||||
return {
|
||||
webpublic: null,
|
||||
thumbnail: null,
|
||||
|
@ -191,8 +191,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
|
|||
thumbnail: null,
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn(`sharp failed: ${e}`);
|
||||
} catch (err) {
|
||||
logger.warn(`sharp failed: ${err}`);
|
||||
return {
|
||||
webpublic: null,
|
||||
thumbnail: null,
|
||||
|
@ -215,8 +215,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
|
|||
} else {
|
||||
logger.debug(`web image not created (not an required image)`);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn(`web image not created (an error occured)`, e);
|
||||
} catch (err) {
|
||||
logger.warn(`web image not created (an error occured)`, err as Error);
|
||||
}
|
||||
} else {
|
||||
logger.info(`web image not created (from remote)`);
|
||||
|
@ -234,8 +234,8 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
|
|||
} else {
|
||||
logger.debug(`thumbnail not created (not an required file)`);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn(`thumbnail not created (an error occured)`, e);
|
||||
} catch (err) {
|
||||
logger.warn(`thumbnail not created (an error occured)`, err as Error);
|
||||
}
|
||||
// #endregion thumbnail
|
||||
|
||||
|
@ -451,9 +451,9 @@ export async function addFile({
|
|||
file.storedInternal = false;
|
||||
|
||||
file = await DriveFiles.insert(file).then(x => DriveFiles.findOneOrFail(x.identifiers[0]));
|
||||
} catch (e) {
|
||||
} catch (err) {
|
||||
// duplicate key error (when already registered)
|
||||
if (isDuplicateKeyValueError(e)) {
|
||||
if (isDuplicateKeyValueError(err)) {
|
||||
logger.info(`already registered ${file.uri}`);
|
||||
|
||||
file = await DriveFiles.findOne({
|
||||
|
@ -461,8 +461,8 @@ export async function addFile({
|
|||
userId: user ? user.id : null,
|
||||
}) as DriveFile;
|
||||
} else {
|
||||
logger.error(e);
|
||||
throw e;
|
||||
logger.error(err as Error);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue