Skip to content

Commit

Permalink
Handle videos
Browse files Browse the repository at this point in the history
  • Loading branch information
allen-ziegenfus committed Oct 30, 2023
1 parent d0a16bf commit 6d2ccf5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion public/werkgruppen.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/components/ImageViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import "yet-another-react-lightbox/styles.css";
export default function ImageViewer({ imgs, title }) {
const [open, setOpen] = useState(false);
const [index, setIndex] = useState(0);

const mainSrc = imgs[0]?.src;
const isVideo = imgs[0]?.src.endsWith(".webm");
return (
<div className="flex flex-row flex-wrap justify-center">
<img src={imgs[0]?.src} alt={title} />
{isVideo && <video src={mainSrc} controls alt={title} />}
{!isVideo && <img src={mainSrc} alt={title} />}
<div className="flex flex-wrap mx-auto align-center justify-center">
{imgs.length > 1 &&
imgs.map((img, index) => (
Expand Down
36 changes: 22 additions & 14 deletions src/werkverzeichnis/werkgruppen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async function performGetWerkgruppen() {
Literatur: record.fields.Literatur,
Bibliographie: record.fields.Bibliographie,
Bilder: bilder,
Thumbnail: bilder[0]
Thumbnail: bilder[0].endsWith(".webm") ? "/placeholder.png" : bilder[0]
};
records.push(work);
}
Expand Down Expand Up @@ -176,31 +176,39 @@ async function downloadFile(url: string, filepath: string): Promise<void> {
async function getAttachmentURL(attachment: any, slug: string, index: number) {
let url = "/placeholder.png";
const base_filename = `${slug}-${String(index).padStart(2, '0')}`;

const isVideo = attachment && attachment.type.startsWith("video");
let attachmentUrl = attachment.url;
if (attachment?.thumbnails?.large?.url) {
const downloadpath = `./build/images/${attachment.filename}`;
const new_filename = `images/${base_filename}.webp`
attachmentUrl = attachment?.thumbnails?.large?.url;
}
if (attachmentUrl) {
const new_filename = isVideo ? `images/${base_filename}.webm` : `images/${base_filename}.webp`
const new_filepath = `./public/${new_filename}`;
const downloadpath = isVideo ? new_filepath : `./build/images/${attachment.filename}`;
try {
let imageExists = await imageDownloaded(downloadpath);
while (!imageExists) {
try {
await downloadFile(attachment?.thumbnails?.large?.url, downloadpath)
await downloadFile(attachmentUrl, downloadpath)
} catch (error) {
console.log(`Axios download for ${downloadpath} failed - trying again: ${error}`)
}
imageExists = await imageDownloaded(downloadpath);
}

const webppath = `./public/${new_filename}`;
imageExists = await imageDownloaded(webppath);
while (!imageExists) {
try {
console.log(`Converting ${downloadpath} to webp ${webppath}`);
await sharp(downloadpath).webp().toFile(webppath);
}
catch (error) {
console.log(`Error converting ${attachment?.thumbnails?.large?.url} ${downloadpath} to ${webppath}: ${error}`)
if (!isVideo) {
imageExists = await imageDownloaded(new_filepath);
while (!imageExists) {
try {
console.log(`Converting ${downloadpath} to webp ${new_filepath}`);
await sharp(downloadpath).webp().toFile(new_filepath);
}
catch (error) {
console.log(`Error converting ${attachmentUrl} ${downloadpath} to ${new_filepath}: ${error}`)
}
imageExists = await imageDownloaded(downloadpath);
}
imageExists = await imageDownloaded(downloadpath);
}
}
catch (error) {
Expand Down

0 comments on commit 6d2ccf5

Please sign in to comment.