Skip to content

Commit

Permalink
fix(media): use placeholder image when videos do not have image details
Browse files Browse the repository at this point in the history
closes #53
  • Loading branch information
BenShelton committed Jun 20, 2021
1 parent 8331046 commit c6ed45e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
45 changes: 37 additions & 8 deletions packages/media/app/main/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Publication,
updateCatalog
} from '@library-api/core'
import { MediaDetailsDTO } from '@library-api/core/types/dto'
import { MediaDetailsDTO, VideoDTO } from '@library-api/core/types/dto'

import { imageExtensions, videoExtensions } from 'shared/src/extensions'
import { initDirectories } from './directories'
Expand All @@ -35,16 +35,16 @@ import {
VideoDetails
} from 'shared/types/ipc'

function getVideoPaths (details: MediaDetailsDTO): { imagePath: string, videoPath: string } {
const srcPath = join(VIDEO_DIR, details.id.replace('#', ''))
function getVideoPaths (id: string): { imagePath: string, videoPath: string } {
const srcPath = join(VIDEO_DIR, id.replace('#', ''))
return {
imagePath: srcPath + '_preview.jpg',
videoPath: srcPath + '_video.mp4'
}
}

async function processVideoDetails (details: MediaDetailsDTO): Promise<VideoDetails> {
const { imagePath, videoPath } = getVideoPaths(details)
const { imagePath, videoPath } = getVideoPaths(details.id)

const imageDownloaded = await checkExists(imagePath)
if (!imageDownloaded) {
Expand All @@ -63,6 +63,28 @@ async function processVideoDetails (details: MediaDetailsDTO): Promise<VideoDeta
}
}

async function placeholderVideoDetails (video: VideoDTO): Promise<VideoDetails> {
const detailsId = video.id + '_placeholder'
const { videoPath } = getVideoPaths(detailsId)
const imageSrc = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjQiIGhlaWdodD0iNjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CgogPGc+CiAgPHRpdGxlPmJhY2tncm91bmQ8L3RpdGxlPgogIDxyZWN0IGZpbGw9Im5vbmUiIGlkPSJjYW52YXNfYmFja2dyb3VuZCIgaGVpZ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EuMywtMS44YzAuNiwtMC42IDEuMiwtMS4yIDIsLTEuOWMwLjcsLTAuNiAxLjMsLTEuMyAxLjcsLTIuMWMwLjUsLTAuOCAwLjcsLTEuNyAwLjcsLTIuNmMwLC0wLjcgLTAuMSwtMS40IC0wLjQsLTJjLTAuMywtMC42IC0wLjcsLTEuMSAtMS4xLC0xLjZzLTEsLTAuOCAtMS43LC0xYy0wLjYsLTAuMiAtMS4zLC0wLjQgLTIsLTAuNGMtMSwwIC0xLjksMC4yIC0yLjYsMC41Yy0wLjgsMC4zIC0xLjQsMC44IC0xLjksMS40Yy0wLjUsMC42IC0wLjksMS4zIC0xLjEsMi4xYy0wLjIsMC44IC0wLjQsMS41IC0wLjMsMi40bC0yLjMsMGMtMC4xLC0xLjQgMCwtMi4zIDAuNCwtMy40bDAsMHptNiwxNy41bDIuOCwwbDAsMi44bC0yLjgsMGwwLC0yLjh6IiBjbGFzcz0ic3QwIi8+CiA8L2c+Cjwvc3ZnPg=='
const downloaded = await checkExists(videoPath)

return {
details: {
id: detailsId,
caption: '(Unknown Video)',
filename: '',
height: 0,
width: 0,
url: ''
},
id: video.id,
src: imageSrc,
text: '(Unknown Video)',
downloaded
}
}

async function sendMedia (src: string): Promise<void> {
const displayWindow = await getDisplayWindow()
displayWindow.webContents.send('display:media', { src } as DisplayMedia['Args'])
Expand Down Expand Up @@ -107,7 +129,14 @@ export function initIPC (): void {
try {
const videos: IPCVideoDTO[] = await Promise.all(baseVideos.map(async (video) => {
const details = await db.getMediaDetails(video)
if (!details) throw new Error(`Cannot load details for video: ${video.id}`)
if (!details) {
log.error(`Cannot load details for video: ${video.id}`)
const videoDetails = await placeholderVideoDetails(video)
return {
...video,
...videoDetails
}
}

const videoDetails = await processVideoDetails(details)

Expand All @@ -128,13 +157,13 @@ export function initIPC (): void {
})

ipcMain.handle('download:video', async (_event, args: DownloadVideo['Args']): Promise<DownloadVideo['Response']> => {
const { videoPath } = getVideoPaths(args.details)
const { videoPath } = getVideoPaths(args.details.id)
const stream = await downloadVideoStream(args, videoPath)
if (!stream) throw new Error(`Could not load video stream for video detail: ${args.details.id}`)
})

ipcMain.handle('download:song', async (_event, args: DownloadSong['Args']): Promise<DownloadSong['Response']> => {
const { videoPath } = getVideoPaths(args.details)
const { videoPath } = getVideoPaths(args.details.id)
const stream = await downloadSongStream(args.track, videoPath, args.languageId)
if (!stream) throw new Error(`Could not load song stream for song: ${args.track}`)
})
Expand Down Expand Up @@ -178,7 +207,7 @@ export function initIPC (): void {
})

ipcMain.on('media:video', async (_event, args: MediaVideo['Args']) => {
const { videoPath } = getVideoPaths(args.details)
const { videoPath } = getVideoPaths(args.details.id)
await sendMedia('file:///' + videoPath)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/media/app/renderer/src/pages/Display.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default defineComponent({
const src = ref<string | null>(null)
const mediaType = ref<'image' | 'video'>('image')
window.electron.on<DisplayMedia>('display:media', (args) => {
if (args.src.startsWith('data:image/jpeg;base64')) {
if (args.src.startsWith('data:image/')) {
mediaType.value = 'image'
} else {
const ext = args.src.split('.').pop()
Expand Down

0 comments on commit c6ed45e

Please sign in to comment.