]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add ability to override client assets : logo - favicon - PWA icons - PWA manifest...
authorKim <1877318+kimsible@users.noreply.github.com>
Fri, 10 Jul 2020 08:20:11 +0000 (10:20 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Jul 2020 08:20:11 +0000 (10:20 +0200)
* Add client-overrides storage to config

* Add static-serve for client overrides

* Move backgroun-image logo from bundle to css tag for runtime content hash

* Add dynamic JSON manifest

* Add content hash for manifest, favicon and logo

Co-authored-by: kimsible <kimsible@users.noreply.github.com>
15 files changed:
client/src/app/app.component.scss
client/src/index.html
config/default.yaml
config/production.yaml.example
config/test-1.yaml
config/test-2.yaml
config/test-3.yaml
config/test-4.yaml
config/test-5.yaml
config/test-6.yaml
server/controllers/client.ts
server/initializers/config.ts
server/initializers/constants.ts
server/lib/client-html.ts
support/docker/production/config/production.yaml

index d121ebad2dac5b4a3be10946ede960c6e04424c5..38ec11b5bd980fc0f03758be1f456318c667b7ca 100644 (file)
@@ -62,7 +62,7 @@
 
       .icon.icon-logo {
         display: inline-block;
-        background: url('../assets/images/logo.svg') no-repeat;
+        background-repeat: no-repeat;
         width: 23px;
         height: 24px;
         margin-right: .5rem;
index 52ae000bb276cac3ff763f56a5575c299ec932f3..e5d1569aae00bb160108db20fd353cdb607734f1 100644 (file)
@@ -7,9 +7,16 @@
     <meta name="theme-color" content="#fff" />
     <meta property="og:platform" content="PeerTube" />
     <!-- Web Manifest file -->
-    <link rel="manifest" href="/manifest.webmanifest">
+    <link rel="manifest" href="/manifest.webmanifest?[manifestContentHash]">
 
-    <link rel="icon" type="image/png" href="/client/assets/images/favicon.png" />
+    <link rel="icon" type="image/png" href="/client/assets/images/favicon.png?[faviconContentHash]" />
+
+    <!-- logo background-image -->
+    <style type="text/css">
+      .icon-logo {
+        background-image: url(/client/assets/images/logo.svg?[logoContentHash]);
+      }
+    </style>
 
     <!-- base url -->
     <base href="/">
index a3df1bd451ad87a3e349e639219257c1b0df7504..d6f7f7afe9d7779b40ded2f2c8a1e74552c80916 100644 (file)
@@ -85,6 +85,11 @@ storage:
   captions: 'storage/captions/'
   cache: 'storage/cache/'
   plugins: 'storage/plugins/'
+  # Overridable client files : logo.svg, favicon.png and icons/*.png (PWA) in client/dist/assets/images
+  # Could contain for example assets/images/favicon.png
+  # If the file exists, peertube will serve it
+  # If not, peertube will fallback to the default fil
+  client_overrides: 'storage/client-overrides/'
 
 log:
   level: 'info' # debug/info/warning/error
index a494bdb039951744fec6eb4a32e6a77415d66799..f57861eca619bf9a1fc290f4b30d092d02f72c29 100644 (file)
@@ -86,6 +86,11 @@ storage:
   captions: '/var/www/peertube/storage/captions/'
   cache: '/var/www/peertube/storage/cache/'
   plugins: '/var/www/peertube/storage/plugins/'
+  # Overridable client files : logo.svg, favicon.png and icons/*.png (PWA) in client/dist/assets/images
+  # Could contain for example assets/images/favicon.png
+  # If the file exists, peertube will serve it
+  # If not, peertube will fallback to the default fil
+  client_overrides: '/var/www/peertube/storage/client-overrides/'
 
 log:
   level: 'info' # debug/info/warning/error
index 7b25f5cf3340a7475d0cae8f2655e4ecdd683163..2ef9e6c7cf9e0b1e7bd24182985e8b124f7d984d 100644 (file)
@@ -22,6 +22,7 @@ storage:
   captions: 'test1/captions/'
   cache: 'test1/cache/'
   plugins: 'test1/plugins/'
+  client_overrides: 'test1/client-overrides/'
 
 admin:
   email: 'admin1@example.com'
index 82d4aa35f37503013e8043b329f7465f2bff89b7..b559769c350c607e95eec60d4994b2d21afd0b59 100644 (file)
@@ -22,6 +22,7 @@ storage:
   captions: 'test2/captions/'
   cache: 'test2/cache/'
   plugins: 'test2/plugins/'
+  client_overrides: 'test2/client-overrides/'
 
 admin:
   email: 'admin2@example.com'
index d2734f469c3f8d7553e2b52718463f1cdeee2940..9a7a944e99ef76bf1d18abea598230d129d6fd1b 100644 (file)
@@ -22,6 +22,7 @@ storage:
   captions: 'test3/captions/'
   cache: 'test3/cache/'
   plugins: 'test3/plugins/'
+  client_overrides: 'test3/client-overrides/'
 
 admin:
   email: 'admin3@example.com'
index 9ec45b024efaea061622e0be617da6656074291a..1e4bee9748d6adbb4f3499099c4fadca470b507d 100644 (file)
@@ -22,6 +22,7 @@ storage:
   captions: 'test4/captions/'
   cache: 'test4/cache/'
   plugins: 'test4/plugins/'
+  client_overrides: 'test4/client-overrides/'
 
 admin:
   email: 'admin4@example.com'
index 92cc113b9bd93a555e58b84f1d254be0ff0420bd..9725e84f42d4777ca3c912f5f82aaf69d0cb23c7 100644 (file)
@@ -22,6 +22,7 @@ storage:
   captions: 'test5/captions/'
   cache: 'test5/cache/'
   plugins: 'test5/plugins/'
+  client_overrides: 'test5/client-overrides/'
 
 admin:
   email: 'admin5@example.com'
index 205d99797ddfb91cd9c9ff2ed5983a8c18a78712..a04c8a6a935b59bd1df1dff92c9dd1b7eb6cc3e4 100644 (file)
@@ -22,6 +22,7 @@ storage:
   captions: 'test6/captions/'
   cache: 'test6/cache/'
   plugins: 'test6/plugins/'
+  client_overrides: 'test6/client-overrides/'
 
 admin:
   email: 'admin6@example.com'
index 65b5a053c9f4004eabe78687feb80c82e79bb412..88f51907b4a2898ae897e5b04d0d9e2e9ba342bf 100644 (file)
@@ -1,3 +1,4 @@
+import { constants, promises as fs } from 'fs'
 import * as express from 'express'
 import { join } from 'path'
 import { root } from '../helpers/core-utils'
@@ -39,20 +40,40 @@ clientsRouter.use(
 )
 
 // Static HTML/CSS/JS client files
-
 const staticClientFiles = [
-  'manifest.webmanifest',
   'ngsw-worker.js',
   'ngsw.json'
 ]
+
 for (const staticClientFile of staticClientFiles) {
   const path = join(root(), 'client', 'dist', staticClientFile)
 
-  clientsRouter.get('/' + staticClientFile, (req: express.Request, res: express.Response) => {
+  clientsRouter.get(`/${staticClientFile}`, (req: express.Request, res: express.Response) => {
     res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
   })
 }
 
+// Dynamic PWA manifest
+clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest))
+
+// Static client overrides
+const staticClientOverrides = [
+  'assets/images/logo.svg',
+  'assets/images/favicon.png',
+  'assets/images/icons/icon-36x36.png',
+  'assets/images/icons/icon-48x48.png',
+  'assets/images/icons/icon-72x72.png',
+  'assets/images/icons/icon-96x96.png',
+  'assets/images/icons/icon-144x144.png',
+  'assets/images/icons/icon-192x192.png',
+  'assets/images/icons/icon-512x512.png'
+]
+
+for (const staticClientOverride of staticClientOverrides) {
+  const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride)
+  clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath)))
+}
+
 clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
 
@@ -130,3 +151,28 @@ function sendHTML (html: string, res: express.Response) {
 
   return res.send(html)
 }
+
+async function generateManifest (req: express.Request, res: express.Response) {
+  const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest')
+  const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8')
+  const manifest = JSON.parse(manifestJson)
+
+  manifest.name = CONFIG.INSTANCE.NAME
+  manifest.short_name = CONFIG.INSTANCE.NAME
+  manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION
+
+  res.json(manifest)
+}
+
+function serveClientOverride (path: string) {
+  return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    try {
+      await fs.access(path, constants.F_OK)
+      // Serve override client
+      res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
+    } catch {
+      // Serve dist client
+      next()
+    }
+  }
+}
index 48e2cbc1a92910ab9cdf602f2adaa3ec5e0c70b1..32bd3bbe2aac33134fcc8861d8e696e38f763500 100644 (file)
@@ -68,7 +68,8 @@ const CONFIG = {
     CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
     TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
     CACHE_DIR: buildPath(config.get<string>('storage.cache')),
-    PLUGINS_DIR: buildPath(config.get<string>('storage.plugins'))
+    PLUGINS_DIR: buildPath(config.get<string>('storage.plugins')),
+    CLIENT_OVERRIDES_DIR: buildPath(config.get<string>('storage.client_overrides'))
   },
   WEBSERVER: {
     SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
index 9a262fd4b5989aba058d42242fafd4b2f118815d..e730e3c84080ddff3ef105e93892d2d773ae83dd 100644 (file)
@@ -1,4 +1,5 @@
 import { join } from 'path'
+import { randomBytes } from 'crypto'
 import { JobType, VideoRateType, VideoResolution, VideoState } from '../../shared/models'
 import { ActivityPubActorType } from '../../shared/models/activitypub'
 import { FollowState } from '../../shared/models/actors'
@@ -710,6 +711,14 @@ registerConfigChangedHandler(() => {
 
 // ---------------------------------------------------------------------------
 
+const FILES_CONTENT_HASH = {
+  MANIFEST: generateContentHash(),
+  FAVICON: generateContentHash(),
+  LOGO: generateContentHash()
+}
+
+// ---------------------------------------------------------------------------
+
 export {
   WEBSERVER,
   API_VERSION,
@@ -792,8 +801,10 @@ export {
   VIDEO_PLAYLIST_PRIVACIES,
   PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
   ASSETS_PATH,
+  FILES_CONTENT_HASH,
   loadLanguages,
-  buildLanguages
+  buildLanguages,
+  generateContentHash
 }
 
 // ---------------------------------------------------------------------------
@@ -895,3 +906,7 @@ function buildLanguages () {
 
   return languages
 }
+
+function generateContentHash () {
+  return randomBytes(20).toString('hex')
+}
index 3e6da2898c1add008c1631ea4b7fdcd8c3c51749..5996f3c70b05975039cc3a6030ea11f34d063bb1 100644 (file)
@@ -1,6 +1,6 @@
 import * as express from 'express'
 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n'
-import { CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, PLUGIN_GLOBAL_CSS_PATH, WEBSERVER } from '../initializers/constants'
+import { CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, PLUGIN_GLOBAL_CSS_PATH, WEBSERVER, FILES_CONTENT_HASH } from '../initializers/constants'
 import { join } from 'path'
 import { escapeHTML, sha256 } from '../helpers/core-utils'
 import { VideoModel } from '../models/video/video'
@@ -101,6 +101,9 @@ export class ClientHtml {
     let html = buffer.toString()
 
     if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang)
+    html = ClientHtml.addManifestContentHash(html)
+    html = ClientHtml.addFaviconContentHash(html)
+    html = ClientHtml.addLogoContentHash(html)
     html = ClientHtml.addCustomCSS(html)
     html = await ClientHtml.addAsyncPluginCSS(html)
 
@@ -136,6 +139,18 @@ export class ClientHtml {
     return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
   }
 
+  private static addManifestContentHash (htmlStringPage: string) {
+    return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
+  }
+
+  private static addFaviconContentHash(htmlStringPage: string) {
+    return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
+  }
+
+  private static addLogoContentHash(htmlStringPage: string) {
+    return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
+  }
+
   private static addTitleTag (htmlStringPage: string, title?: string) {
     let text = title || CONFIG.INSTANCE.NAME
     if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
index 4eeca41106133cc70d1b24c95fa9103788cbfcd0..a32cf1a89038de64e822b9f6eecaed2b1ecc2baf 100644 (file)
@@ -54,6 +54,11 @@ storage:
   captions: '../data/captions/'
   cache: '../data/cache/'
   plugins: '../data/plugins/'
+  # Overridable client files : logo.svg, favicon.png and icons/*.png (PWA) in client/dist/assets/images
+  # Could contain for example assets/images/favicon.png
+  # If the file exists, peertube will serve it
+  # If not, peertube will fallback to the default fil
+  client_overrides: '../data/client-overrides/'
 
 log:
   level: 'info' # debug/info/warning/error