Skip to content

Commit

Permalink
Update image server to no longer spit out Airtable URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
slmnio committed Mar 9, 2023
1 parent 110e7a0 commit a2112b2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
9 changes: 6 additions & 3 deletions server/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,16 @@ async function removeAttachmentTimestamps(data) {
let { ending, filename } = getAutoFilename(attachment);
attachment._autoFilename = filename;
attachment.fileExtension = ending;
attachments.set(attachment.id, attachment);
attachments.set(attachment.id, {...attachment});

attachment.url = generateAttachmentURL(attachment.url, attachment);
// we don't want the URLs to appear in requests anymore
// the data server just uses the attachment IDs

attachment.url = null; // generateAttachmentURL(attachment.url, attachment);

for (let size in attachment.thumbnails) {
size = attachment.thumbnails[size];
size.url = generateAttachmentURL(size.url, attachment);
size.url = null; // generateAttachmentURL(size.url, attachment);
}
});
}
Expand Down
8 changes: 7 additions & 1 deletion server/src/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ async function downloadImage(url, filename, size) {
file.on("finish", () => file.close(resolve));
}).on("error", err => {
console.error(`[image] file error for ${filename} ${err.code} ${err.message}`);
fs.unlink(pathName);
fs.unlink(pathName, function(err) {
if (err) {
console.error(`[image] file error for ${filename} unlink FAILED`, err);
} else {
console.error(`[image] file error for ${filename} unlinked`);
}
});
reject(err);
});
}));
Expand Down
9 changes: 5 additions & 4 deletions server/src/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ function getFileEnding(url) {
}

const dataServer = process.env.NODE_ENV === "development" ? "http://localhost:8901" : "https://data.slmn.gg";
function getResizedImage(airtableURL, size = "s-500") {
// just using orig for now
return `${dataServer}/image.${getFileEnding(airtableURL) || "png"}?size=orig&url=${encodeURIComponent(airtableURL.replace("?", "&"))}`;

function getImageURL(attachment, size = "orig") {
return `${dataServer}/image.${attachment.fileExtension}?id=${attachment.id}&size=${size}`;
}


function aImg(airtableImage, size) {
// console.log(airtableImage);
if (!airtableImage || !airtableImage.length) return null;
Expand All @@ -25,7 +26,7 @@ function aImg(airtableImage, size) {
width: i.width,
height: i.height,
type: i.type,
url: getResizedImage(i.url, size)
url: getImageURL(i.url, size)
};
}
function themeSquare(id, size = 500) {
Expand Down
4 changes: 2 additions & 2 deletions website/src/components/broadcast/MapSegment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<script>
import { logoBackground } from "@/utils/theme-styles";
import { bg, getAirtableURL, resizedImage } from "@/utils/images";
import { bg, getNewURL, resizedImage } from "@/utils/images";
import ThemeLogo from "@/components/website/ThemeLogo";
Expand All @@ -61,7 +61,7 @@ export default {
if (!(image)) return {};
try {
return bg(getAirtableURL(image));
return bg(getNewURL(image, "orig"));
} catch (e) {
return {};
}
Expand Down
4 changes: 2 additions & 2 deletions website/src/components/broadcast/roots/AdReadOverlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<script>
import { ReactiveArray, ReactiveThing } from "@/utils/reactive";
import { bg, getAirtableURL } from "@/utils/images";
import { bg, getNewURL } from "@/utils/images";
async function wait(ms) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -43,7 +43,7 @@ export default {
readImage() {
if (!this.activeRead) return {};
try {
return bg(getAirtableURL(this.activeRead.image));
return bg(getNewURL(this.activeRead.image, "orig"));
} catch (e) {
return {};
}
Expand Down
10 changes: 3 additions & 7 deletions website/src/utils/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ export function getNewURL(attachment, size) {
return `${dataServer}/image.${attachment.fileExtension}?id=${attachment.id}&size=${size}`;
}

export function getAirtableURL(attachment) {
if (attachment?.url) return attachment.url;
return attachment?.[0]?.url || null;
}

function keyedImageAttachments(theme, keys) {
const urls = keys.map(key => theme[key]?.[0]).filter(s => s?.url);
if (urls.length) return urls[0];
const attachments = keys.map(key => theme[key]?.[0]).filter(s => s?.id);
if (attachments.length) return attachments[0];
return null;
}

export function resizedImageNoWrap(theme, keys, size) {
// console.log("resized image", { theme, keys, size });
if (!theme) return null;
const imageAttachment = keyedImageAttachments(theme, keys);
if (!imageAttachment) return null;
Expand Down

0 comments on commit a2112b2

Please sign in to comment.