unoffical wafrn mirror wafrn.net
atproto social-network activitypub
at cache-folder-container 47 lines 1.6 kB view raw
1import crypto from "node:crypto"; 2import fs from "fs"; 3import axios from "axios"; 4import type { Job } from "bullmq"; 5import mime from "mime"; 6import { Media } from "../../models/index.js"; 7import { completeEnvironment } from "../backendOptions.js"; 8import { fileTypeFromFile } from "file-type"; 9import sharp from "sharp"; 10import { Model } from "sequelize"; 11import { redisCache } from "../redis.js"; 12import { getMediaFromUrl } from "../../routes/remoteCache.js"; 13 14async function processRemoteMedia(job: Job) { 15 try { 16 const media = await Media.findByPk(job.data.mediaId); 17 if (!media) return; 18 const mediaUrl = media.external 19 ? media.url 20 : completeEnvironment.mediaUrl + media.url; 21 let fileLocation = ""; 22 await getMediaFromUrl(mediaUrl); 23 // get the local file name from redis using the hash of the media url 24 const mediaLinkHash = crypto 25 .createHash("sha256") 26 .update(mediaUrl) 27 .digest("hex"); 28 const localFilename = `cache/${mediaLinkHash}`; 29 fileLocation = localFilename; 30 31 if (fs.existsSync(fileLocation)) { 32 const fileType = await fileTypeFromFile(fileLocation); 33 if (fileType?.mime) { 34 media.mediaType = fileType?.mime; 35 if (fileType.mime.startsWith("image")) { 36 const metadata = await sharp(fileLocation).metadata(); 37 media.height = metadata.height || 0; 38 media.width = metadata.width || 0; 39 media.updatedAt = new Date(); 40 } 41 await media.save(); 42 } 43 } 44 } catch (error) {} 45} 46 47export { processRemoteMedia };