From 14e2014acc1362cfbb770c051a7254b156cd8efb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 11 Dec 2018 14:52:50 +0100 Subject: Support additional video extensions --- server/initializers/checker-before-init.ts | 2 +- server/initializers/constants.ts | 96 +++++++++++++++------- .../migrations/0295-video-file-extname.ts | 49 +++++++++++ 3 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 server/initializers/migrations/0295-video-file-extname.ts (limited to 'server/initializers') diff --git a/server/initializers/checker-before-init.ts b/server/initializers/checker-before-init.ts index b51c7cfba..a7bc7eec8 100644 --- a/server/initializers/checker-before-init.ts +++ b/server/initializers/checker-before-init.ts @@ -19,7 +19,7 @@ function checkMissedConfig () { 'signup.enabled', 'signup.limit', 'signup.requires_email_verification', 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist', 'redundancy.videos.strategies', 'redundancy.videos.check_interval', - 'transcoding.enabled', 'transcoding.threads', + 'transcoding.enabled', 'transcoding.threads', 'transcoding.allow_additional_extensions', 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'trending.videos.interval_days', 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index d4496bc34..ad61bee73 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -16,7 +16,7 @@ let config: IConfig = require('config') // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 290 +const LAST_MIGRATION_VERSION = 295 // --------------------------------------------------------------------------- @@ -246,6 +246,7 @@ const CONFIG = { }, TRANSCODING: { get ENABLED () { return config.get('transcoding.enabled') }, + get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get('transcoding.allow_additional_extensions') }, get THREADS () { return config.get('transcoding.threads') }, RESOLUTIONS: { get '240p' () { return config.get('transcoding.resolutions.240p') }, @@ -298,7 +299,7 @@ const CONFIG = { // --------------------------------------------------------------------------- -const CONSTRAINTS_FIELDS = { +let CONSTRAINTS_FIELDS = { USERS: { NAME: { min: 1, max: 50 }, // Length DESCRIPTION: { min: 3, max: 1000 }, // Length @@ -357,7 +358,7 @@ const CONSTRAINTS_FIELDS = { max: 2 * 1024 * 1024 // 2MB } }, - EXTNAME: [ '.mp4', '.ogv', '.webm' ], + EXTNAME: buildVideosExtname(), INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2 DURATION: { min: 0 }, // Number TAGS: { min: 0, max: 5 }, // Number of total tags @@ -480,27 +481,31 @@ const VIDEO_ABUSE_STATES = { [VideoAbuseState.ACCEPTED]: 'Accepted' } -const VIDEO_MIMETYPE_EXT = { - 'video/webm': '.webm', - 'video/ogg': '.ogv', - 'video/mp4': '.mp4' -} -const VIDEO_EXT_MIMETYPE = invert(VIDEO_MIMETYPE_EXT) - -const IMAGE_MIMETYPE_EXT = { - 'image/png': '.png', - 'image/jpg': '.jpg', - 'image/jpeg': '.jpg' -} - -const VIDEO_CAPTIONS_MIMETYPE_EXT = { - 'text/vtt': '.vtt', - 'application/x-subrip': '.srt' -} - -const TORRENT_MIMETYPE_EXT = { - 'application/x-bittorrent': '.torrent' +const MIMETYPES = { + VIDEO: { + MIMETYPE_EXT: buildVideoMimetypeExt(), + EXT_MIMETYPE: null as { [ id: string ]: string } + }, + IMAGE: { + MIMETYPE_EXT: { + 'image/png': '.png', + 'image/jpg': '.jpg', + 'image/jpeg': '.jpg' + } + }, + VIDEO_CAPTIONS: { + MIMETYPE_EXT: { + 'text/vtt': '.vtt', + 'application/x-subrip': '.srt' + } + }, + TORRENT: { + MIMETYPE_EXT: { + 'application/x-bittorrent': '.torrent' + } + } } +MIMETYPES.VIDEO.EXT_MIMETYPE = invert(MIMETYPES.VIDEO.MIMETYPE_EXT) // --------------------------------------------------------------------------- @@ -526,7 +531,7 @@ const ACTIVITY_PUB = { COLLECTION_ITEMS_PER_PAGE: 10, FETCH_PAGE_LIMIT: 100, URL_MIME_TYPES: { - VIDEO: Object.keys(VIDEO_MIMETYPE_EXT), + VIDEO: Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT), TORRENT: [ 'application/x-bittorrent' ], MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ] }, @@ -685,13 +690,12 @@ if (isTestInstance() === true) { ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS = '0ms' } -updateWebserverConfig() +updateWebserverUrls() // --------------------------------------------------------------------------- export { API_VERSION, - VIDEO_CAPTIONS_MIMETYPE_EXT, AVATARS_SIZE, ACCEPT_HEADERS, BCRYPT_SALT_SIZE, @@ -719,7 +723,6 @@ export { FEEDS, JOB_TTL, NSFW_POLICY_TYPES, - TORRENT_MIMETYPE_EXT, STATIC_MAX_AGE, STATIC_PATHS, VIDEO_IMPORT_TIMEOUT, @@ -732,7 +735,6 @@ export { VIDEO_LICENCES, VIDEO_STATES, VIDEO_RATE_TYPES, - VIDEO_MIMETYPE_EXT, VIDEO_TRANSCODING_FPS, FFMPEG_NICE, VIDEO_ABUSE_STATES, @@ -740,13 +742,12 @@ export { USER_PASSWORD_RESET_LIFETIME, MEMOIZE_TTL, USER_EMAIL_VERIFY_LIFETIME, - IMAGE_MIMETYPE_EXT, OVERVIEWS, SCHEDULER_INTERVALS_MS, REPEAT_JOBS, STATIC_DOWNLOAD_PATHS, RATES_LIMIT, - VIDEO_EXT_MIMETYPE, + MIMETYPES, CRAWL_REQUEST_CONCURRENCY, JOB_COMPLETED_LIFETIME, HTTP_SIGNATURE, @@ -768,11 +769,43 @@ function getLocalConfigFilePath () { return join(dirname(configSources[ 0 ].name), filename + '.json') } -function updateWebserverConfig () { +function buildVideoMimetypeExt () { + const data = { + 'video/webm': '.webm', + 'video/ogg': '.ogv', + 'video/mp4': '.mp4' + } + + if (CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) { + Object.assign(data, { + 'video/quicktime': '.mov', + 'video/x-msvideo': '.avi', + 'video/x-flv': '.flv', + 'video/x-matroska': '.mkv' + }) + } + + return data +} + +function updateWebserverUrls () { CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT) CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP) } +function updateWebserverConfig () { + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = buildVideosExtname() + + MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt() + MIMETYPES.VIDEO.EXT_MIMETYPE = invert(MIMETYPES.VIDEO.MIMETYPE_EXT) +} + +function buildVideosExtname () { + return CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS + ? [ '.mp4', '.ogv', '.webm', '.mkv', '.mov', '.avi', '.flv' ] + : [ '.mp4', '.ogv', '.webm' ] +} + function buildVideosRedundancy (objs: any[]): VideosRedundancy[] { if (!objs) return [] @@ -854,4 +887,5 @@ export function reloadConfig () { config = require('config') updateWebserverConfig() + updateWebserverUrls() } diff --git a/server/initializers/migrations/0295-video-file-extname.ts b/server/initializers/migrations/0295-video-file-extname.ts new file mode 100644 index 000000000..dbf249f66 --- /dev/null +++ b/server/initializers/migrations/0295-video-file-extname.ts @@ -0,0 +1,49 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize, + db: any +}): Promise { + { + await utils.queryInterface.renameColumn('videoFile', 'extname', 'extname_old') + } + + { + const data = { + type: Sequelize.STRING, + defaultValue: null, + allowNull: true + } + + await utils.queryInterface.addColumn('videoFile', 'extname', data) + } + + { + const query = 'UPDATE "videoFile" SET "extname" = "extname_old"::text' + await utils.sequelize.query(query) + } + + { + const data = { + type: Sequelize.STRING, + defaultValue: null, + allowNull: false + } + await utils.queryInterface.changeColumn('videoFile', 'extname', data) + } + + { + await utils.queryInterface.removeColumn('videoFile', 'extname_old') + } +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} -- cgit v1.2.3