From 15a7eafb892441957ba7dd6fcbf556086fe5b2b3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 26 Jul 2021 15:04:37 +0200 Subject: Refactor video links builders --- server/controllers/api/server/logs.ts | 3 +-- server/controllers/bots.ts | 16 ++++++++-------- server/controllers/feeds.ts | 2 +- server/helpers/logger.ts | 23 ++++++++++++++++++++++- server/initializers/constants.ts | 2 +- server/lib/schedulers/plugins-check-scheduler.ts | 10 +++++----- server/models/video/video-playlist.ts | 8 ++++---- server/models/video/video.ts | 7 ++++--- 8 files changed, 46 insertions(+), 25 deletions(-) (limited to 'server') diff --git a/server/controllers/api/server/logs.ts b/server/controllers/api/server/logs.ts index f78607d35..39eceb654 100644 --- a/server/controllers/api/server/logs.ts +++ b/server/controllers/api/server/logs.ts @@ -1,8 +1,7 @@ import * as express from 'express' import { readdir, readFile } from 'fs-extra' import { join } from 'path' -import { logger } from '@server/helpers/logger' -import { mtimeSortFilesDesc } from '../../../../shared/core-utils/logs/logs' +import { logger, mtimeSortFilesDesc } from '@server/helpers/logger' import { LogLevel } from '../../../../shared/models/server/log-level.type' import { UserRight } from '../../../../shared/models/users' import { CONFIG } from '../../../initializers/config' diff --git a/server/controllers/bots.ts b/server/controllers/bots.ts index 93aa0cf30..de0411608 100644 --- a/server/controllers/bots.ts +++ b/server/controllers/bots.ts @@ -1,13 +1,13 @@ import * as express from 'express' -import { asyncMiddleware } from '../middlewares' -import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' +import { truncate } from 'lodash' import { SitemapStream, streamToPromise } from 'sitemap' +import { buildNSFWFilter } from '../helpers/express-utils' +import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' +import { asyncMiddleware } from '../middlewares' +import { cacheRoute } from '../middlewares/cache/cache' +import { AccountModel } from '../models/account/account' import { VideoModel } from '../models/video/video' import { VideoChannelModel } from '../models/video/video-channel' -import { AccountModel } from '../models/account/account' -import { cacheRoute } from '../middlewares/cache/cache' -import { buildNSFWFilter } from '../helpers/express-utils' -import { truncate } from 'lodash' const botsRouter = express.Router() @@ -75,13 +75,13 @@ async function getSitemapLocalVideoUrls () { }) return data.map(v => ({ - url: WEBSERVER.URL + '/w/' + v.uuid, + url: WEBSERVER.URL + v.getWatchStaticPath(), video: [ { title: v.name, // Sitemap description should be < 2000 characters description: truncate(v.description || v.name, { length: 2000, omission: '...' }), - player_loc: WEBSERVER.URL + '/videos/embed/' + v.uuid, + player_loc: WEBSERVER.URL + v.getEmbedStaticPath(), thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath() } ] diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index cdc6bfb8b..9fa70a7c8 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts @@ -286,7 +286,7 @@ function addVideosToFeed (feed, videos: VideoModel[]) { feed.addItem({ title: video.name, id: video.url, - link: WEBSERVER.URL + '/w/' + video.uuid, + link: WEBSERVER.URL + video.getWatchStaticPath(), description: video.getTruncatedDescription(), content: video.description, author: [ diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index 29e06860d..20c3c3edb 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts @@ -1,5 +1,5 @@ // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ -import { mkdirpSync } from 'fs-extra' +import { mkdirpSync, stat } from 'fs-extra' import { omit } from 'lodash' import * as path from 'path' import { format as sqlFormat } from 'sql-formatter' @@ -158,6 +158,26 @@ function loggerTagsFactory (...defaultTags: string[]): LoggerTagsFn { } } +async function mtimeSortFilesDesc (files: string[], basePath: string) { + const promises = [] + const out: { file: string, mtime: number }[] = [] + + for (const file of files) { + const p = stat(basePath + '/' + file) + .then(stats => { + if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() }) + }) + + promises.push(p) + } + + await Promise.all(promises) + + out.sort((a, b) => b.mtime - a.mtime) + + return out +} + // --------------------------------------------------------------------------- export { @@ -168,6 +188,7 @@ export { labelFormatter, consoleLoggerFormat, jsonLoggerFormat, + mtimeSortFilesDesc, logger, loggerTagsFactory, bunyanLogger diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index ee4503b2c..5f121d9a4 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -2,7 +2,7 @@ import { CronRepeatOptions, EveryRepeatOptions } from 'bull' import { randomBytes } from 'crypto' import { invert } from 'lodash' import { join } from 'path' -import { randomInt } from '../../shared/core-utils/miscs/miscs' +import { randomInt } from '../../shared/core-utils/common/miscs' import { AbuseState, JobType, diff --git a/server/lib/schedulers/plugins-check-scheduler.ts b/server/lib/schedulers/plugins-check-scheduler.ts index 9a1ae3ec5..c95e109b0 100644 --- a/server/lib/schedulers/plugins-check-scheduler.ts +++ b/server/lib/schedulers/plugins-check-scheduler.ts @@ -1,12 +1,12 @@ +import { chunk } from 'lodash' +import { compareSemVer } from '@shared/core-utils' import { logger } from '../../helpers/logger' -import { AbstractScheduler } from './abstract-scheduler' -import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants' import { CONFIG } from '../../initializers/config' +import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants' import { PluginModel } from '../../models/server/plugin' -import { chunk } from 'lodash' -import { getLatestPluginsVersion } from '../plugins/plugin-index' -import { compareSemVer } from '../../../shared/core-utils/miscs/miscs' import { Notifier } from '../notifier' +import { getLatestPluginsVersion } from '../plugins/plugin-index' +import { AbstractScheduler } from './abstract-scheduler' export class PluginsCheckScheduler extends AbstractScheduler { diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index af81c9906..245475f94 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -20,7 +20,7 @@ import { import { setAsUpdated } from '@server/helpers/database-utils' import { buildUUID, uuidToShort } from '@server/helpers/uuid' import { MAccountId, MChannelId } from '@server/types/models' -import { AttributesOnly } from '@shared/core-utils' +import { AttributesOnly, buildPlaylistEmbedPath, buildPlaylistLink, buildPlaylistWatchPath } from '@shared/core-utils' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' @@ -560,12 +560,12 @@ export class VideoPlaylistModel extends Model>> { } getWatchStaticPath () { - return '/w/' + this.uuid + return buildVideoWatchPath({ shortUUID: shortToUUID(this.uuid) }) } getEmbedStaticPath () { - return '/videos/embed/' + this.uuid + return buildVideoEmbedPath(this) } getMiniatureStaticPath () { -- cgit v1.2.3