Add video player
This commit is contained in:
1 parent
7be2508eac
commit
65eeba2df6
7 files changed
+157
-54
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 3.4 KiB |
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* lightgallery | 2.8.0-beta.3 | May 3rd 2024
|
||||
* http://www.lightgalleryjs.com/
|
||||
* Copyright (c) 2020 Sachin Neravath;
|
||||
* @license GPLv3
|
||||
*/
|
||||
|
||||
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(e="undefined"!=typeof globalThis?globalThis:e||self).lgVideo=o()}(this,(function(){"use strict";var e=function(){return(e=Object.assign||function(e){for(var o,i=1,t=arguments.length;i<t;i++)for(var s in o=arguments[i])Object.prototype.hasOwnProperty.call(o,s)&&(e[s]=o[s]);return e}).apply(this,arguments)},o={autoplayFirstVideo:!0,youTubePlayerParams:!1,vimeoPlayerParams:!1,wistiaPlayerParams:!1,gotoNextSlideOnVideoEnd:!0,autoplayVideoOnSlide:!1,videojs:!1,videojsTheme:"",videojsOptions:{}},i="lgHasVideo",t="lgSlideItemLoad",s="lgBeforeSlide",n="lgAfterSlide",l="lgPosterClick",r=function(e){return Object.keys(e).map((function(o){return encodeURIComponent(o)+"="+encodeURIComponent(e[o])})).join("&")},d=function(o,i){if(!o.youtube)return"";var t=o.youtube[2]?o.youtube[2].slice(1).split("&").map((function(e){return e.split("=")})).reduce((function(e,o){var i=o.map(decodeURIComponent),t=i[0],s=i[1];return e[t]=s,e}),{}):"",s=i||{},n=e(e(e({},{wmode:"opaque",autoplay:0,mute:1,enablejsapi:1}),s),t);return"?"+r(n)};return function(){function a(i){return this.core=i,this.settings=e(e({},o),this.core.settings),this}return a.prototype.init=function(){var e=this;this.core.LGel.on(i+".video",this.onHasVideo.bind(this)),this.core.LGel.on(l+".video",(function(){var o=e.core.getSlideItem(e.core.index);e.loadVideoOnPosterClick(o)})),this.core.LGel.on(t+".video",this.onSlideItemLoad.bind(this)),this.core.LGel.on(s+".video",this.onBeforeSlide.bind(this)),this.core.LGel.on(n+".video",this.onAfterSlide.bind(this))},a.prototype.onSlideItemLoad=function(e){var o=this,i=e.detail,t=i.isFirstSlide,s=i.index;this.settings.autoplayFirstVideo&&t&&s===this.core.index&&setTimeout((function(){o.loadAndPlayVideo(s)}),200),!t&&this.settings.autoplayVideoOnSlide&&s===this.core.index&&this.loadAndPlayVideo(s)},a.prototype.onHasVideo=function(e){var o=e.detail,i=o.index,t=o.src,s=o.html5Video;o.hasPoster||(this.appendVideos(this.core.getSlideItem(i),{src:t,addClass:"lg-object",index:i,html5Video:s}),this.gotoNextSlideOnVideoEnd(t,i))},a.prototype.onBeforeSlide=function(e){if(this.core.lGalleryOn){var o=e.detail.prevIndex;this.pauseVideo(o)}},a.prototype.onAfterSlide=function(e){var o=this,i=e.detail,t=i.index,s=i.prevIndex,n=this.core.getSlideItem(t);this.settings.autoplayVideoOnSlide&&t!==s&&n.hasClass("lg-complete")&&setTimeout((function(){o.loadAndPlayVideo(t)}),100)},a.prototype.loadAndPlayVideo=function(e){var o=this.core.getSlideItem(e);this.core.galleryItems[e].poster?this.loadVideoOnPosterClick(o,!0):this.playVideo(e)},a.prototype.playVideo=function(e){this.controlVideo(e,"play")},a.prototype.pauseVideo=function(e){this.controlVideo(e,"pause")},a.prototype.getVideoHtml=function(e,o,i,t){var s="",n=this.core.galleryItems[i].__slideVideoInfo||{},l=this.core.galleryItems[i],a=l.title||l.alt;a=a?'title="'+a+'"':"";var c='allowtransparency="true"\n frameborder="0"\n scrolling="no"\n allowfullscreen\n mozallowfullscreen\n webkitallowfullscreen\n oallowfullscreen\n msallowfullscreen';if(n.youtube){var u="lg-youtube"+i,f=d(n,this.settings.youTubePlayerParams);s='<iframe allow="autoplay" id='+u+' class="lg-video-object lg-youtube '+o+'" '+a+' src="'+(e.includes("youtube-nocookie.com")?"//www.youtube-nocookie.com/":"//www.youtube.com/")+"embed/"+(n.youtube[1]+f)+'" '+c+"></iframe>"}else if(n.vimeo){u="lg-vimeo"+i;var h=function(e,o){if(!o||!o.vimeo)return"";var i=o.vimeo[2]||"",t=Object.assign({},{autoplay:0,muted:1},e),s=t&&0!==Object.keys(t).length?r(t):"",n=((o.vimeo[0].split("/").pop()||"").split("?")[0]||"").split("#")[0],l=o.vimeo[1]!==n;l&&(i=i.replace("/"+n,""));var d=l?"h="+n:"";return"?"+d+(s=d?"&"+s:s)+("?"==i[0]?"&"+i.slice(1):i||"")}(this.settings.vimeoPlayerParams,n);s='<iframe allow="autoplay" id='+u+' class="lg-video-object lg-vimeo '+o+'" '+a+' src="//player.vimeo.com/video/'+(n.vimeo[1]+h)+'" '+c+"></iframe>"}else if(n.wistia){var g="lg-wistia"+i;h=(h=r(this.settings.wistiaPlayerParams))?"?"+h:"",s='<iframe allow="autoplay" id="'+g+'" src="//fast.wistia.net/embed/iframe/'+(n.wistia[4]+h)+'" '+a+' class="wistia_embed lg-video-object lg-wistia '+o+'" name="wistia_embed" '+c+"></iframe>"}else if(n.html5){for(var p="",y=0;y<t.source.length;y++){var v=t.source[y].type,m=v?'type="'+v+'"':"";p+='<source src="'+t.source[y].src+'" '+m+">"}if(t.tracks){var w=function(e){var o="",i=t.tracks[e];Object.keys(i||{}).forEach((function(e){o+=e+'="'+i[e]+'" '})),p+="<track "+o+">"};for(y=0;y<t.tracks.length;y++)w(y)}var V="",b=t.attributes||{};Object.keys(b||{}).forEach((function(e){V+=e+'="'+b[e]+'" '})),s='<video class="lg-video-object lg-html5 '+(this.settings.videojs&&this.settings.videojsTheme?this.settings.videojsTheme+" ":"")+" "+(this.settings.videojs?" video-js":"")+'" '+V+">\n "+p+"\n Your browseLine truncated
|
||||
+25
-6
@@ -2,13 +2,32 @@ html {
|
||||
background: #191919;
|
||||
}
|
||||
|
||||
#lightgallery a {
|
||||
text-decoration: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#lightgallery img {
|
||||
height: 250px;
|
||||
margin: 4px;
|
||||
}
|
||||
|
||||
#lightgallery a {
|
||||
position: relative; /* Establishes a positioning context */
|
||||
display: inline-block; /* Allows the container to wrap around the image */
|
||||
text-decoration: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.play-icon {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
background-image: url('/images/play.png');
|
||||
background-size: contain;
|
||||
background-repeat: no-repeat;
|
||||
opacity: 0.5;
|
||||
}
|
||||
#lightgallery a:has(.play-icon):hover .play-icon {
|
||||
opacity: 1;
|
||||
}
|
||||
+23
-9
@@ -1,14 +1,15 @@
|
||||
import { Asset, AssetType, ImageSize, SharedLink } from './types'
|
||||
|
||||
class Immich {
|
||||
async request (endpoint: string, json = true) {
|
||||
async request (endpoint: string) {
|
||||
const res = await fetch(process.env.IMMICH_URL + '/api' + endpoint, {
|
||||
headers: {
|
||||
'x-api-key': process.env.API_KEY || ''
|
||||
}
|
||||
})
|
||||
if (res.status === 200) {
|
||||
if (json) {
|
||||
const contentType = res.headers.get('Content-Type') || ''
|
||||
if (contentType.includes('application/json')) {
|
||||
return res.json()
|
||||
} else {
|
||||
return res
|
||||
@@ -17,25 +18,38 @@ class Immich {
|
||||
}
|
||||
|
||||
async getShareByKey (key: string) {
|
||||
const links = await this.request('/shared-links') as SharedLink[]
|
||||
return links.find(x => x.key === key)
|
||||
const res = (await this.request('/shared-links') || []) as SharedLink[]
|
||||
return res?.find(x => x.key === key)
|
||||
}
|
||||
|
||||
async getAssetBuffer (asset: Asset, size?: ImageSize) {
|
||||
switch (asset.type) {
|
||||
case AssetType.image:
|
||||
size = size === ImageSize.thumbnail ? ImageSize.thumbnail : ImageSize.original
|
||||
return this.request('/assets/' + asset.id + '/' + size, false)
|
||||
return this.request('/assets/' + asset.id + '/' + size)
|
||||
case AssetType.video:
|
||||
return this.request('/assets/' + asset.id + '/video/playback', false)
|
||||
return this.request('/assets/' + asset.id + '/video/playback')
|
||||
}
|
||||
}
|
||||
|
||||
imageUrl (id: string, size?: ImageSize) {
|
||||
async getContentType (asset: Asset) {
|
||||
const assetBuffer = await this.getAssetBuffer(asset)
|
||||
return assetBuffer.headers.get('Content-Type')
|
||||
}
|
||||
|
||||
photoUrl (id: string, size?: ImageSize) {
|
||||
return `${process.env.SERVER_URL}/photo/${id}` + (size ? `?size=${size}` : '')
|
||||
}
|
||||
|
||||
videoUrl (id: string) {
|
||||
return `${process.env.SERVER_URL}/video/${id}`
|
||||
}
|
||||
|
||||
isId (id: string) {
|
||||
return !!id.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
|
||||
}
|
||||
}
|
||||
|
||||
const api = new Immich()
|
||||
const immich = new Immich()
|
||||
|
||||
export default api
|
||||
export default immich
|
||||
+26
-33
@@ -1,9 +1,9 @@
|
||||
import express from 'express'
|
||||
import api from './immich'
|
||||
import immich from './immich'
|
||||
import render from './render'
|
||||
import dayjs from 'dayjs'
|
||||
import { AssetType, ImageSize } from './types'
|
||||
import { Request, Response } from 'express-serve-static-core'
|
||||
import { Request } from 'express-serve-static-core'
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
@@ -11,22 +11,6 @@ const app = express()
|
||||
app.set('view engine', 'ejs')
|
||||
app.use(express.static('public'))
|
||||
|
||||
async function serveImage (res: Response, id: string, size?: ImageSize) {
|
||||
const image = await api.getAssetBuffer({
|
||||
id,
|
||||
type: AssetType.image
|
||||
}, size)
|
||||
if (image) {
|
||||
for (const header of ['content-type', 'content-length']) {
|
||||
res.set(header, image.headers[header])
|
||||
}
|
||||
console.log(`${dayjs().format()} Serving image ${id}`)
|
||||
res.send(Buffer.from(await image.arrayBuffer()))
|
||||
} else {
|
||||
res.status(404).send()
|
||||
}
|
||||
}
|
||||
|
||||
const getSize = (req: Request) => {
|
||||
return req?.query?.size === 'thumbnail' ? ImageSize.thumbnail : ImageSize.original
|
||||
}
|
||||
@@ -36,33 +20,42 @@ app.get('/share/:key', async (req, res) => {
|
||||
// Invalid characters in the incoming URL
|
||||
res.status(404).send()
|
||||
} else {
|
||||
const share = await api.getShareByKey(req.params.key)
|
||||
const share = await immich.getShareByKey(req.params.key)
|
||||
if (!share || !share.assets.length) {
|
||||
res.status(404).send()
|
||||
} else if (share.assets.length === 1) {
|
||||
// This is an individual item (not a gallery)
|
||||
await serveImage(res, share.assets[0].id, getSize(req))
|
||||
const asset = share.assets[0]
|
||||
if (asset.type === AssetType.image) {
|
||||
// Output the image directly
|
||||
await render.assetBuffer(res, share.assets[0], getSize(req))
|
||||
} else if (asset.type === AssetType.video) {
|
||||
// Show the video as a web player
|
||||
await render.gallery(res, share.assets, 1)
|
||||
}
|
||||
} else {
|
||||
// Multiple images - render as a gallery
|
||||
res.render('gallery', {
|
||||
photos: share.assets.map(photo => {
|
||||
return {
|
||||
originalUrl: immich.imageUrl(photo.id),
|
||||
thumbnailUrl: immich.imageUrl(photo.id, ImageSize.thumbnail)
|
||||
}
|
||||
})
|
||||
})
|
||||
await render.gallery(res, share.assets, 1)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/photo/:id', (req, res) => {
|
||||
if (req.params.id.match(/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/)) {
|
||||
// Check for photo
|
||||
serveImage(res, req.params.id, getSize(req)).then()
|
||||
} else {
|
||||
// Output the buffer data for an photo or video
|
||||
app.get('/:type(photo|video)/:id', (req, res) => {
|
||||
if (!immich.isId(req.params.id)) {
|
||||
// Invalid characters in the incoming URL
|
||||
res.status(404).send()
|
||||
return
|
||||
}
|
||||
const asset = {
|
||||
id: req.params.id,
|
||||
type: req.params.type === 'video' ? AssetType.video : AssetType.image
|
||||
}
|
||||
switch (req.params.type) {
|
||||
case 'photo':
|
||||
case 'video':
|
||||
render.assetBuffer(res, asset, getSize(req)).then()
|
||||
break
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import immich from './immich'
|
||||
import { Response } from 'express-serve-static-core'
|
||||
import { Asset, AssetType, ImageSize } from './types'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
class Render {
|
||||
async assetBuffer (res: Response, asset: Asset, size?: ImageSize) {
|
||||
const data = await immich.getAssetBuffer(asset, size)
|
||||
if (data) {
|
||||
for (const header of ['content-type', 'content-length']) {
|
||||
res.set(header, data.headers[header])
|
||||
}
|
||||
console.log(`${dayjs().format()} Serving asset ${asset.id}`)
|
||||
res.send(Buffer.from(await data.arrayBuffer()))
|
||||
} else {
|
||||
res.status(404).send()
|
||||
}
|
||||
}
|
||||
|
||||
async gallery (res: Response, assets: Asset[], openItem?: number) {
|
||||
const items = []
|
||||
for (const asset of assets) {
|
||||
let video
|
||||
if (asset.type === AssetType.video) {
|
||||
// Populate the data-video property
|
||||
video = JSON.stringify({
|
||||
source: [
|
||||
{
|
||||
src: immich.videoUrl(asset.id),
|
||||
type: await immich.getContentType(asset)
|
||||
}
|
||||
],
|
||||
attributes: {
|
||||
preload: false,
|
||||
controls: true
|
||||
}
|
||||
})
|
||||
}
|
||||
items.push({
|
||||
originalUrl: immich.photoUrl(asset.id),
|
||||
thumbnailUrl: immich.photoUrl(asset.id, ImageSize.thumbnail),
|
||||
video
|
||||
})
|
||||
}
|
||||
res.render('gallery', {
|
||||
items,
|
||||
openItem
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const render = new Render()
|
||||
|
||||
export default render
|
||||
+21
-6
@@ -7,21 +7,36 @@
|
||||
</head>
|
||||
<body>
|
||||
<div id="lightgallery">
|
||||
<% photos.forEach(photo => { %>
|
||||
<a href="<%- photo.originalUrl %>">
|
||||
<img alt="" src="<%- photo.thumbnailUrl %>" />
|
||||
<% items.forEach(item => {
|
||||
if (item.video) { %>
|
||||
<a data-video='<%- item.video %>'>
|
||||
<img alt="" src="<%- item.thumbnailUrl %>"/>
|
||||
<div class="play-icon"></div>
|
||||
</a>
|
||||
<% }) %>
|
||||
<% } else { %>
|
||||
<a href="<%- item.originalUrl %>">
|
||||
<img alt="" src="<%- item.thumbnailUrl %>"/>
|
||||
</a>
|
||||
<% }
|
||||
}) %>
|
||||
</div>
|
||||
<script src="/lightgallery.min.js"></script>
|
||||
<script src="/lg-thumbnail.min.js"></script>
|
||||
<script src="/lg-video.min.js"></script>
|
||||
<script src="/lg-zoom.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
lightGallery(document.getElementById('lightgallery'), {
|
||||
plugins: [lgZoom, lgThumbnail],
|
||||
plugins: [lgZoom, lgThumbnail, lgVideo],
|
||||
licenseKey: '0000-0000-000-0000',
|
||||
speed: 500
|
||||
});
|
||||
})
|
||||
<% if (openItem) { %>
|
||||
const openItem = <%- openItem %>
|
||||
const thumbs = document.querySelectorAll('#lightgallery a')
|
||||
if (thumbs.length >= openItem) {
|
||||
thumbs[openItem - 1].click()
|
||||
}
|
||||
<% } %>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user