constants.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. import { IConfig } from 'config'
  2. import { dirname, join } from 'path'
  3. import { JobType, VideoRateType, VideoState, VideosRedundancy } from '../../shared/models'
  4. import { ActivityPubActorType } from '../../shared/models/activitypub'
  5. import { FollowState } from '../../shared/models/actors'
  6. import { VideoAbuseState, VideoImportState, VideoPrivacy, VideoTranscodingFPS } from '../../shared/models/videos'
  7. // Do not use barrels, remain constants as independent as possible
  8. import { buildPath, isTestInstance, parseDuration, parseBytes, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
  9. import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
  10. import { invert } from 'lodash'
  11. import { CronRepeatOptions, EveryRepeatOptions } from 'bull'
  12. import * as bytes from 'bytes'
  13. // Use a variable to reload the configuration if we need
  14. let config: IConfig = require('config')
  15. // ---------------------------------------------------------------------------
  16. const LAST_MIGRATION_VERSION = 285
  17. // ---------------------------------------------------------------------------
  18. // API version
  19. const API_VERSION = 'v1'
  20. const PAGINATION = {
  21. COUNT: {
  22. DEFAULT: 15,
  23. MAX: 100
  24. }
  25. }
  26. // Sortable columns per schema
  27. const SORTABLE_COLUMNS = {
  28. USERS: [ 'id', 'username', 'createdAt' ],
  29. USER_SUBSCRIPTIONS: [ 'id', 'createdAt' ],
  30. ACCOUNTS: [ 'createdAt' ],
  31. JOBS: [ 'createdAt' ],
  32. VIDEO_ABUSES: [ 'id', 'createdAt', 'state' ],
  33. VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
  34. VIDEO_IMPORTS: [ 'createdAt' ],
  35. VIDEO_COMMENT_THREADS: [ 'createdAt' ],
  36. BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
  37. FOLLOWERS: [ 'createdAt' ],
  38. FOLLOWING: [ 'createdAt' ],
  39. VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes', 'trending' ],
  40. VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes', 'match' ],
  41. VIDEO_CHANNELS_SEARCH: [ 'match', 'displayName', 'createdAt' ],
  42. ACCOUNTS_BLOCKLIST: [ 'createdAt' ],
  43. SERVERS_BLOCKLIST: [ 'createdAt' ]
  44. }
  45. const OAUTH_LIFETIME = {
  46. ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
  47. REFRESH_TOKEN: 1209600 // 2 weeks
  48. }
  49. const ROUTE_CACHE_LIFETIME = {
  50. FEEDS: '15 minutes',
  51. ROBOTS: '2 hours',
  52. SECURITYTXT: '2 hours',
  53. NODEINFO: '10 minutes',
  54. DNT_POLICY: '1 week',
  55. OVERVIEWS: {
  56. VIDEOS: '1 hour'
  57. },
  58. ACTIVITY_PUB: {
  59. VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
  60. },
  61. STATS: '4 hours'
  62. }
  63. // ---------------------------------------------------------------------------
  64. // Number of points we add/remove after a successful/bad request
  65. const ACTOR_FOLLOW_SCORE = {
  66. PENALTY: -10,
  67. BONUS: 10,
  68. BASE: 1000,
  69. MAX: 10000
  70. }
  71. const FOLLOW_STATES: { [ id: string ]: FollowState } = {
  72. PENDING: 'pending',
  73. ACCEPTED: 'accepted'
  74. }
  75. const REMOTE_SCHEME = {
  76. HTTP: 'https',
  77. WS: 'wss'
  78. }
  79. const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
  80. 'activitypub-http-broadcast': 5,
  81. 'activitypub-http-unicast': 5,
  82. 'activitypub-http-fetcher': 5,
  83. 'activitypub-follow': 5,
  84. 'video-file-import': 1,
  85. 'video-file': 1,
  86. 'video-import': 1,
  87. 'email': 5,
  88. 'videos-views': 1
  89. }
  90. const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
  91. 'activitypub-http-broadcast': 1,
  92. 'activitypub-http-unicast': 5,
  93. 'activitypub-http-fetcher': 1,
  94. 'activitypub-follow': 3,
  95. 'video-file-import': 1,
  96. 'video-file': 1,
  97. 'video-import': 1,
  98. 'email': 5,
  99. 'videos-views': 1
  100. }
  101. const JOB_TTL: { [ id in JobType ]: number } = {
  102. 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
  103. 'activitypub-http-unicast': 60000 * 10, // 10 minutes
  104. 'activitypub-http-fetcher': 60000 * 10, // 10 minutes
  105. 'activitypub-follow': 60000 * 10, // 10 minutes
  106. 'video-file-import': 1000 * 3600, // 1 hour
  107. 'video-file': 1000 * 3600 * 48, // 2 days, transcoding could be long
  108. 'video-import': 1000 * 3600 * 2, // hours
  109. 'email': 60000 * 10, // 10 minutes
  110. 'videos-views': undefined // Unlimited
  111. }
  112. const REPEAT_JOBS: { [ id: string ]: EveryRepeatOptions | CronRepeatOptions } = {
  113. 'videos-views': {
  114. cron: '1 * * * *' // At 1 minutes past the hour
  115. }
  116. }
  117. const BROADCAST_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-http-broadcast job
  118. const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
  119. const JOB_REQUEST_TIMEOUT = 3000 // 3 seconds
  120. const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
  121. const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
  122. // 1 hour
  123. let SCHEDULER_INTERVALS_MS = {
  124. badActorFollow: 60000 * 60, // 1 hour
  125. removeOldJobs: 60000 * 60, // 1 hour
  126. updateVideos: 60000, // 1 minute
  127. youtubeDLUpdate: 60000 * 60 * 24 // 1 day
  128. }
  129. // ---------------------------------------------------------------------------
  130. const CONFIG = {
  131. CUSTOM_FILE: getLocalConfigFilePath(),
  132. LISTEN: {
  133. PORT: config.get<number>('listen.port'),
  134. HOSTNAME: config.get<string>('listen.hostname')
  135. },
  136. DATABASE: {
  137. DBNAME: 'peertube' + config.get<string>('database.suffix'),
  138. HOSTNAME: config.get<string>('database.hostname'),
  139. PORT: config.get<number>('database.port'),
  140. USERNAME: config.get<string>('database.username'),
  141. PASSWORD: config.get<string>('database.password'),
  142. POOL: {
  143. MAX: config.get<number>('database.pool.max')
  144. }
  145. },
  146. REDIS: {
  147. HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
  148. PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
  149. SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
  150. AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
  151. DB: config.has('redis.db') ? config.get<number>('redis.db') : null
  152. },
  153. SMTP: {
  154. HOSTNAME: config.get<string>('smtp.hostname'),
  155. PORT: config.get<number>('smtp.port'),
  156. USERNAME: config.get<string>('smtp.username'),
  157. PASSWORD: config.get<string>('smtp.password'),
  158. TLS: config.get<boolean>('smtp.tls'),
  159. DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
  160. CA_FILE: config.get<string>('smtp.ca_file'),
  161. FROM_ADDRESS: config.get<string>('smtp.from_address')
  162. },
  163. STORAGE: {
  164. AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
  165. LOG_DIR: buildPath(config.get<string>('storage.logs')),
  166. VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
  167. THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
  168. PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
  169. CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
  170. TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
  171. CACHE_DIR: buildPath(config.get<string>('storage.cache'))
  172. },
  173. WEBSERVER: {
  174. SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
  175. WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
  176. HOSTNAME: config.get<string>('webserver.hostname'),
  177. PORT: config.get<number>('webserver.port'),
  178. URL: '',
  179. HOST: ''
  180. },
  181. TRUST_PROXY: config.get<string[]>('trust_proxy'),
  182. LOG: {
  183. LEVEL: config.get<string>('log.level')
  184. },
  185. SEARCH: {
  186. REMOTE_URI: {
  187. USERS: config.get<boolean>('search.remote_uri.users'),
  188. ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous')
  189. }
  190. },
  191. TRENDING: {
  192. VIDEOS: {
  193. INTERVAL_DAYS: config.get<number>('trending.videos.interval_days')
  194. }
  195. },
  196. REDUNDANCY: {
  197. VIDEOS: {
  198. CHECK_INTERVAL: parseDuration(config.get<string>('redundancy.videos.check_interval')),
  199. STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
  200. }
  201. },
  202. ADMIN: {
  203. get EMAIL () { return config.get<string>('admin.email') }
  204. },
  205. SIGNUP: {
  206. get ENABLED () { return config.get<boolean>('signup.enabled') },
  207. get LIMIT () { return config.get<number>('signup.limit') },
  208. get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
  209. FILTERS: {
  210. CIDR: {
  211. get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
  212. get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
  213. }
  214. }
  215. },
  216. USER: {
  217. get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
  218. get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
  219. },
  220. TRANSCODING: {
  221. get ENABLED () { return config.get<boolean>('transcoding.enabled') },
  222. get THREADS () { return config.get<number>('transcoding.threads') },
  223. RESOLUTIONS: {
  224. get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
  225. get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
  226. get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
  227. get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
  228. get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
  229. }
  230. },
  231. IMPORT: {
  232. VIDEOS: {
  233. HTTP: {
  234. get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }
  235. },
  236. TORRENT: {
  237. get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
  238. }
  239. }
  240. },
  241. CACHE: {
  242. PREVIEWS: {
  243. get SIZE () { return config.get<number>('cache.previews.size') }
  244. },
  245. VIDEO_CAPTIONS: {
  246. get SIZE () { return config.get<number>('cache.captions.size') }
  247. }
  248. },
  249. INSTANCE: {
  250. get NAME () { return config.get<string>('instance.name') },
  251. get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
  252. get DESCRIPTION () { return config.get<string>('instance.description') },
  253. get TERMS () { return config.get<string>('instance.terms') },
  254. get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
  255. get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
  256. CUSTOMIZATIONS: {
  257. get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
  258. get CSS () { return config.get<string>('instance.customizations.css') }
  259. },
  260. get ROBOTS () { return config.get<string>('instance.robots') },
  261. get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
  262. get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
  263. },
  264. SERVICES: {
  265. TWITTER: {
  266. get USERNAME () { return config.get<string>('services.twitter.username') },
  267. get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
  268. }
  269. }
  270. }
  271. // ---------------------------------------------------------------------------
  272. const CONSTRAINTS_FIELDS = {
  273. USERS: {
  274. NAME: { min: 3, max: 120 }, // Length
  275. DESCRIPTION: { min: 3, max: 1000 }, // Length
  276. USERNAME: { min: 3, max: 20 }, // Length
  277. PASSWORD: { min: 6, max: 255 }, // Length
  278. VIDEO_QUOTA: { min: -1 },
  279. VIDEO_QUOTA_DAILY: { min: -1 },
  280. BLOCKED_REASON: { min: 3, max: 250 } // Length
  281. },
  282. VIDEO_ABUSES: {
  283. REASON: { min: 2, max: 300 }, // Length
  284. MODERATION_COMMENT: { min: 2, max: 300 } // Length
  285. },
  286. VIDEO_BLACKLIST: {
  287. REASON: { min: 2, max: 300 } // Length
  288. },
  289. VIDEO_CHANNELS: {
  290. NAME: { min: 3, max: 120 }, // Length
  291. DESCRIPTION: { min: 3, max: 1000 }, // Length
  292. SUPPORT: { min: 3, max: 1000 }, // Length
  293. URL: { min: 3, max: 2000 } // Length
  294. },
  295. VIDEO_CAPTIONS: {
  296. CAPTION_FILE: {
  297. EXTNAME: [ '.vtt', '.srt' ],
  298. FILE_SIZE: {
  299. max: 2 * 1024 * 1024 // 2MB
  300. }
  301. }
  302. },
  303. VIDEO_IMPORTS: {
  304. URL: { min: 3, max: 2000 }, // Length
  305. TORRENT_NAME: { min: 3, max: 255 }, // Length
  306. TORRENT_FILE: {
  307. EXTNAME: [ '.torrent' ],
  308. FILE_SIZE: {
  309. max: 1024 * 200 // 200 KB
  310. }
  311. }
  312. },
  313. VIDEOS_REDUNDANCY: {
  314. URL: { min: 3, max: 2000 } // Length
  315. },
  316. VIDEOS: {
  317. NAME: { min: 3, max: 120 }, // Length
  318. LANGUAGE: { min: 1, max: 10 }, // Length
  319. TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
  320. DESCRIPTION: { min: 3, max: 10000 }, // Length
  321. SUPPORT: { min: 3, max: 1000 }, // Length
  322. IMAGE: {
  323. EXTNAME: [ '.jpg', '.jpeg' ],
  324. FILE_SIZE: {
  325. max: 2 * 1024 * 1024 // 2MB
  326. }
  327. },
  328. EXTNAME: [ '.mp4', '.ogv', '.webm' ],
  329. INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
  330. DURATION: { min: 0 }, // Number
  331. TAGS: { min: 0, max: 5 }, // Number of total tags
  332. TAG: { min: 2, max: 30 }, // Length
  333. THUMBNAIL: { min: 2, max: 30 },
  334. THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
  335. VIEWS: { min: 0 },
  336. LIKES: { min: 0 },
  337. DISLIKES: { min: 0 },
  338. FILE_SIZE: { min: 10 },
  339. URL: { min: 3, max: 2000 } // Length
  340. },
  341. ACTORS: {
  342. PUBLIC_KEY: { min: 10, max: 5000 }, // Length
  343. PRIVATE_KEY: { min: 10, max: 5000 }, // Length
  344. URL: { min: 3, max: 2000 }, // Length
  345. AVATAR: {
  346. EXTNAME: [ '.png', '.jpeg', '.jpg' ],
  347. FILE_SIZE: {
  348. max: 2 * 1024 * 1024 // 2MB
  349. }
  350. }
  351. },
  352. VIDEO_EVENTS: {
  353. COUNT: { min: 0 }
  354. },
  355. VIDEO_COMMENTS: {
  356. TEXT: { min: 1, max: 3000 }, // Length
  357. URL: { min: 3, max: 2000 } // Length
  358. },
  359. VIDEO_SHARE: {
  360. URL: { min: 3, max: 2000 } // Length
  361. }
  362. }
  363. const RATES_LIMIT = {
  364. LOGIN: {
  365. WINDOW_MS: 5 * 60 * 1000, // 5 minutes
  366. MAX: 15 // 15 attempts
  367. },
  368. ASK_SEND_EMAIL: {
  369. WINDOW_MS: 5 * 60 * 1000, // 5 minutes
  370. MAX: 3 // 3 attempts
  371. }
  372. }
  373. let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
  374. const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
  375. MIN: 10,
  376. AVERAGE: 30,
  377. MAX: 60,
  378. KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
  379. }
  380. const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
  381. LIKE: 'like',
  382. DISLIKE: 'dislike'
  383. }
  384. const FFMPEG_NICE: { [ id: string ]: number } = {
  385. THUMBNAIL: 2, // 2 just for don't blocking servers
  386. TRANSCODING: 15
  387. }
  388. const VIDEO_CATEGORIES = {
  389. 1: 'Music',
  390. 2: 'Films',
  391. 3: 'Vehicles',
  392. 4: 'Art',
  393. 5: 'Sports',
  394. 6: 'Travels',
  395. 7: 'Gaming',
  396. 8: 'People',
  397. 9: 'Comedy',
  398. 10: 'Entertainment',
  399. 11: 'News & Politics',
  400. 12: 'How To',
  401. 13: 'Education',
  402. 14: 'Activism',
  403. 15: 'Science & Technology',
  404. 16: 'Animals',
  405. 17: 'Kids',
  406. 18: 'Food'
  407. }
  408. // See https://creativecommons.org/licenses/?lang=en
  409. const VIDEO_LICENCES = {
  410. 1: 'Attribution',
  411. 2: 'Attribution - Share Alike',
  412. 3: 'Attribution - No Derivatives',
  413. 4: 'Attribution - Non Commercial',
  414. 5: 'Attribution - Non Commercial - Share Alike',
  415. 6: 'Attribution - Non Commercial - No Derivatives',
  416. 7: 'Public Domain Dedication'
  417. }
  418. const VIDEO_LANGUAGES = buildLanguages()
  419. const VIDEO_PRIVACIES = {
  420. [VideoPrivacy.PUBLIC]: 'Public',
  421. [VideoPrivacy.UNLISTED]: 'Unlisted',
  422. [VideoPrivacy.PRIVATE]: 'Private'
  423. }
  424. const VIDEO_STATES = {
  425. [VideoState.PUBLISHED]: 'Published',
  426. [VideoState.TO_TRANSCODE]: 'To transcode',
  427. [VideoState.TO_IMPORT]: 'To import'
  428. }
  429. const VIDEO_IMPORT_STATES = {
  430. [VideoImportState.FAILED]: 'Failed',
  431. [VideoImportState.PENDING]: 'Pending',
  432. [VideoImportState.SUCCESS]: 'Success'
  433. }
  434. const VIDEO_ABUSE_STATES = {
  435. [VideoAbuseState.PENDING]: 'Pending',
  436. [VideoAbuseState.REJECTED]: 'Rejected',
  437. [VideoAbuseState.ACCEPTED]: 'Accepted'
  438. }
  439. const VIDEO_MIMETYPE_EXT = {
  440. 'video/webm': '.webm',
  441. 'video/ogg': '.ogv',
  442. 'video/mp4': '.mp4'
  443. }
  444. const VIDEO_EXT_MIMETYPE = invert(VIDEO_MIMETYPE_EXT)
  445. const IMAGE_MIMETYPE_EXT = {
  446. 'image/png': '.png',
  447. 'image/jpg': '.jpg',
  448. 'image/jpeg': '.jpg'
  449. }
  450. const VIDEO_CAPTIONS_MIMETYPE_EXT = {
  451. 'text/vtt': '.vtt',
  452. 'application/x-subrip': '.srt'
  453. }
  454. const TORRENT_MIMETYPE_EXT = {
  455. 'application/x-bittorrent': '.torrent'
  456. }
  457. // ---------------------------------------------------------------------------
  458. const OVERVIEWS = {
  459. VIDEOS: {
  460. SAMPLE_THRESHOLD: 6,
  461. SAMPLES_COUNT: 2
  462. }
  463. }
  464. // ---------------------------------------------------------------------------
  465. const SERVER_ACTOR_NAME = 'peertube'
  466. const ACTIVITY_PUB = {
  467. POTENTIAL_ACCEPT_HEADERS: [
  468. 'application/activity+json',
  469. 'application/ld+json',
  470. 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
  471. ],
  472. ACCEPT_HEADER: 'application/activity+json, application/ld+json',
  473. PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
  474. COLLECTION_ITEMS_PER_PAGE: 10,
  475. FETCH_PAGE_LIMIT: 100,
  476. URL_MIME_TYPES: {
  477. VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
  478. TORRENT: [ 'application/x-bittorrent' ],
  479. MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
  480. },
  481. MAX_RECURSION_COMMENTS: 100,
  482. ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000, // 1 day
  483. VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
  484. }
  485. const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
  486. GROUP: 'Group',
  487. PERSON: 'Person',
  488. APPLICATION: 'Application'
  489. }
  490. const HTTP_SIGNATURE = {
  491. HEADER_NAME: 'signature',
  492. ALGORITHM: 'rsa-sha256',
  493. HEADERS_TO_SIGN: [ 'date', 'host', 'digest', '(request-target)' ]
  494. }
  495. // ---------------------------------------------------------------------------
  496. const PRIVATE_RSA_KEY_SIZE = 2048
  497. // Password encryption
  498. const BCRYPT_SALT_SIZE = 10
  499. const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
  500. const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
  501. const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = {
  502. DO_NOT_LIST: 'do_not_list',
  503. BLUR: 'blur',
  504. DISPLAY: 'display'
  505. }
  506. // ---------------------------------------------------------------------------
  507. // Express static paths (router)
  508. const STATIC_PATHS = {
  509. PREVIEWS: '/static/previews/',
  510. THUMBNAILS: '/static/thumbnails/',
  511. TORRENTS: '/static/torrents/',
  512. WEBSEED: '/static/webseed/',
  513. AVATARS: '/static/avatars/',
  514. VIDEO_CAPTIONS: '/static/video-captions/'
  515. }
  516. const STATIC_DOWNLOAD_PATHS = {
  517. TORRENTS: '/download/torrents/',
  518. VIDEOS: '/download/videos/'
  519. }
  520. // Cache control
  521. let STATIC_MAX_AGE = '2h'
  522. // Videos thumbnail size
  523. const THUMBNAILS_SIZE = {
  524. width: 200,
  525. height: 110
  526. }
  527. const PREVIEWS_SIZE = {
  528. width: 560,
  529. height: 315
  530. }
  531. const AVATARS_SIZE = {
  532. width: 120,
  533. height: 120
  534. }
  535. const EMBED_SIZE = {
  536. width: 560,
  537. height: 315
  538. }
  539. // Sub folders of cache directory
  540. const CACHE = {
  541. PREVIEWS: {
  542. DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
  543. MAX_AGE: 1000 * 3600 * 3 // 3 hours
  544. },
  545. VIDEO_CAPTIONS: {
  546. DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
  547. MAX_AGE: 1000 * 3600 * 3 // 3 hours
  548. }
  549. }
  550. const MEMOIZE_TTL = {
  551. OVERVIEWS_SAMPLE: 1000 * 3600 * 4 // 4 hours
  552. }
  553. const REDUNDANCY = {
  554. VIDEOS: {
  555. RANDOMIZED_FACTOR: 5
  556. }
  557. }
  558. const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
  559. // ---------------------------------------------------------------------------
  560. const CUSTOM_HTML_TAG_COMMENTS = {
  561. TITLE: '<!-- title tag -->',
  562. DESCRIPTION: '<!-- description tag -->',
  563. CUSTOM_CSS: '<!-- custom css tag -->',
  564. OPENGRAPH_AND_OEMBED: '<!-- open graph and oembed tags -->'
  565. }
  566. // ---------------------------------------------------------------------------
  567. const FEEDS = {
  568. COUNT: 20
  569. }
  570. // ---------------------------------------------------------------------------
  571. const TRACKER_RATE_LIMITS = {
  572. INTERVAL: 60000 * 5, // 5 minutes
  573. ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
  574. ANNOUNCES_PER_IP: 30 // maximum announces for all our torrents in the interval
  575. }
  576. // ---------------------------------------------------------------------------
  577. // Special constants for a test instance
  578. if (isTestInstance() === true) {
  579. ACTOR_FOLLOW_SCORE.BASE = 20
  580. REMOTE_SCHEME.HTTP = 'http'
  581. REMOTE_SCHEME.WS = 'ws'
  582. STATIC_MAX_AGE = '0'
  583. ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
  584. ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
  585. ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
  586. CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
  587. SCHEDULER_INTERVALS_MS.badActorFollow = 10000
  588. SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
  589. SCHEDULER_INTERVALS_MS.updateVideos = 5000
  590. REPEAT_JOBS['videos-views'] = { every: 5000 }
  591. REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
  592. VIDEO_VIEW_LIFETIME = 1000 // 1 second
  593. JOB_ATTEMPTS['email'] = 1
  594. CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
  595. MEMOIZE_TTL.OVERVIEWS_SAMPLE = 1
  596. ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS = '0ms'
  597. }
  598. updateWebserverConfig()
  599. // ---------------------------------------------------------------------------
  600. export {
  601. API_VERSION,
  602. VIDEO_CAPTIONS_MIMETYPE_EXT,
  603. AVATARS_SIZE,
  604. ACCEPT_HEADERS,
  605. BCRYPT_SALT_SIZE,
  606. TRACKER_RATE_LIMITS,
  607. CACHE,
  608. CONFIG,
  609. CONSTRAINTS_FIELDS,
  610. EMBED_SIZE,
  611. REDUNDANCY,
  612. JOB_CONCURRENCY,
  613. JOB_ATTEMPTS,
  614. LAST_MIGRATION_VERSION,
  615. OAUTH_LIFETIME,
  616. CUSTOM_HTML_TAG_COMMENTS,
  617. BROADCAST_CONCURRENCY,
  618. PAGINATION,
  619. ACTOR_FOLLOW_SCORE,
  620. PREVIEWS_SIZE,
  621. REMOTE_SCHEME,
  622. FOLLOW_STATES,
  623. SERVER_ACTOR_NAME,
  624. PRIVATE_RSA_KEY_SIZE,
  625. ROUTE_CACHE_LIFETIME,
  626. SORTABLE_COLUMNS,
  627. FEEDS,
  628. JOB_TTL,
  629. NSFW_POLICY_TYPES,
  630. TORRENT_MIMETYPE_EXT,
  631. STATIC_MAX_AGE,
  632. STATIC_PATHS,
  633. VIDEO_IMPORT_TIMEOUT,
  634. ACTIVITY_PUB,
  635. ACTIVITY_PUB_ACTOR_TYPES,
  636. THUMBNAILS_SIZE,
  637. VIDEO_CATEGORIES,
  638. VIDEO_LANGUAGES,
  639. VIDEO_PRIVACIES,
  640. VIDEO_LICENCES,
  641. VIDEO_STATES,
  642. VIDEO_RATE_TYPES,
  643. VIDEO_MIMETYPE_EXT,
  644. VIDEO_TRANSCODING_FPS,
  645. FFMPEG_NICE,
  646. VIDEO_ABUSE_STATES,
  647. JOB_REQUEST_TIMEOUT,
  648. USER_PASSWORD_RESET_LIFETIME,
  649. MEMOIZE_TTL,
  650. USER_EMAIL_VERIFY_LIFETIME,
  651. IMAGE_MIMETYPE_EXT,
  652. OVERVIEWS,
  653. SCHEDULER_INTERVALS_MS,
  654. REPEAT_JOBS,
  655. STATIC_DOWNLOAD_PATHS,
  656. RATES_LIMIT,
  657. VIDEO_EXT_MIMETYPE,
  658. CRAWL_REQUEST_CONCURRENCY,
  659. JOB_COMPLETED_LIFETIME,
  660. HTTP_SIGNATURE,
  661. VIDEO_IMPORT_STATES,
  662. VIDEO_VIEW_LIFETIME,
  663. buildLanguages
  664. }
  665. // ---------------------------------------------------------------------------
  666. function getLocalConfigFilePath () {
  667. const configSources = config.util.getConfigSources()
  668. if (configSources.length === 0) throw new Error('Invalid config source.')
  669. let filename = 'local'
  670. if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
  671. if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
  672. return join(dirname(configSources[ 0 ].name), filename + '.json')
  673. }
  674. function updateWebserverConfig () {
  675. CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
  676. CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
  677. }
  678. function buildVideosRedundancy (objs: any[]): VideosRedundancy[] {
  679. if (!objs) return []
  680. return objs.map(obj => {
  681. return Object.assign(obj, {
  682. minLifetime: parseDuration(obj.min_lifetime),
  683. size: bytes.parse(obj.size),
  684. minViews: obj.min_views
  685. })
  686. })
  687. }
  688. function buildLanguages () {
  689. const iso639 = require('iso-639-3')
  690. const languages: { [ id: string ]: string } = {}
  691. const additionalLanguages = {
  692. 'sgn': true, // Sign languages (macro language)
  693. 'ase': true, // American sign language
  694. 'sdl': true, // Arabian sign language
  695. 'bfi': true, // British sign language
  696. 'bzs': true, // Brazilian sign language
  697. 'csl': true, // Chinese sign language
  698. 'cse': true, // Czech sign language
  699. 'dsl': true, // Danish sign language
  700. 'fsl': true, // French sign language
  701. 'gsg': true, // German sign language
  702. 'pks': true, // Pakistan sign language
  703. 'jsl': true, // Japanese sign language
  704. 'sfs': true, // South African sign language
  705. 'swl': true, // Swedish sign language
  706. 'rsl': true, // Russian sign language: true
  707. 'epo': true, // Esperanto
  708. 'tlh': true, // Klingon
  709. 'jbo': true, // Lojban
  710. 'avk': true // Kotava
  711. }
  712. // Only add ISO639-1 languages and some sign languages (ISO639-3)
  713. iso639
  714. .filter(l => {
  715. return (l.iso6391 !== null && l.type === 'living') ||
  716. additionalLanguages[l.iso6393] === true
  717. })
  718. .forEach(l => languages[l.iso6391 || l.iso6393] = l.name)
  719. // Override Occitan label
  720. languages['oc'] = 'Occitan'
  721. return languages
  722. }
  723. export function reloadConfig () {
  724. function directory () {
  725. if (process.env.NODE_CONFIG_DIR) {
  726. return process.env.NODE_CONFIG_DIR
  727. }
  728. return join(root(), 'config')
  729. }
  730. function purge () {
  731. for (const fileName in require.cache) {
  732. if (-1 === fileName.indexOf(directory())) {
  733. continue
  734. }
  735. delete require.cache[fileName]
  736. }
  737. delete require.cache[require.resolve('config')]
  738. }
  739. purge()
  740. config = require('config')
  741. updateWebserverConfig()
  742. }