unoffical wafrn mirror wafrn.net
atproto social-network activitypub
at testPDSNotExplode 44 lines 1.5 kB view raw
1import crypto from 'node:crypto' 2import fs from 'node:fs/promises' 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' 12 13async function processRemoteMedia(job: Job) { 14 const media = await Media.findByPk(job.data.mediaId) 15 if (!media) return 16 17 let fileLocation = '' 18 if (media.external) { 19 // call the local cache endpoint to populate redis 20 const cacheUrl = completeEnvironment.frontendUrl + '/api/cache/?media=' + encodeURIComponent(media.url) 21 await axios.get(cacheUrl) 22 23 // get the local file name from redis using the hash of the media url 24 const mediaLinkHash = crypto.createHash('sha256').update(media.url).digest('hex') 25 const localFilename = `cache/${mediaLinkHash}` 26 fileLocation = localFilename! 27 } else { 28 fileLocation = `uploads${media.url}` 29 } 30 const fileType = await fileTypeFromFile(fileLocation) 31 32 if (fileType?.mime) { 33 media.mediaType = fileType?.mime 34 if (fileType.mime.startsWith('image')) { 35 const metadata = await sharp(fileLocation).metadata() 36 media.height = metadata.height || 0 37 media.width = metadata.width || 0 38 media.updatedAt = new Date() 39 } 40 await media.save() 41 } 42} 43 44export { processRemoteMedia }