@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type";
2323import { HTTPError } from "lambert-server" ;
2424import crypto from "crypto" ;
2525import { multer } from "../util/multer" ;
26- import { cache } from "../util/cache" ;
26+ import { cache , cacheNotFound } from "../util/cache" ;
2727import { FileStorage } from "@spacebar/cdn" ;
2828import fs from "fs/promises" ;
2929
@@ -69,7 +69,8 @@ router.get("/:emoji_id", cache, async (req: Request, res: Response) => {
6969 emoji_id = emoji_id . split ( "." ) [ 0 ] ; // remove .file extension
7070 const path = `${ pathPrefix } /${ emoji_id } ` ;
7171
72- const file = await getOrMoveFile ( path , `avatars/${ emoji_id } ` ) ;
72+ const file = await storage . get ( path ) ;
73+ if ( ! file ) return cacheNotFound ( req , res ) ;
7374 const type = await fileTypeFromBuffer ( file ) ;
7475
7576 res . set ( "Content-Type" , type ?. mime ) ;
@@ -87,35 +88,4 @@ router.delete("/:emoji_id", async (req: Request, res: Response) => {
8788 return res . send ( { success : true } ) ;
8889} ) ;
8990
90- async function getOrMoveFile ( newPath : string , oldPath : string ) {
91- let file = await storage . get ( newPath ) ;
92- if ( file ) {
93- if ( ! ( await storage . isFile ( newPath ) ) ) {
94- console . log ( `[CDN] Migrating emoji from subdirectory+fallback to direct path for ${ newPath } ` ) ;
95- // noinspection SuspiciousTypeOfGuard -- not sure whats up with this
96- if ( storage instanceof FileStorage ) {
97- const files = await fs . readdir ( storage . getFsPath ( newPath ) ) ;
98- if ( files . length === 1 ) {
99- const oldFilePath = storage . getFsPath ( `${ newPath } /${ files [ 0 ] } ` ) ;
100- const newFilePath = storage . getFsPath ( newPath ) ;
101- await fs . rename ( oldFilePath , newFilePath + ".tmp" ) ;
102- await fs . rmdir ( storage . getFsPath ( newPath ) ) ;
103- await fs . rename ( newFilePath + ".tmp" , newFilePath ) ;
104- file = await storage . get ( newPath ) ;
105- } else console . log ( `[CDN] Warning: not migrating emojis ${ newPath } , as there are multiple files in the old directory` ) ;
106- } else {
107- console . log ( "[CDN] Warning: no migration for s3 storage emojis, as it is not a filesystem" ) ;
108- }
109- }
110- } else {
111- if ( await storage . exists ( oldPath ) ) {
112- console . log ( `[${ pathPrefix } ] found file at old path ${ oldPath } , moving to new path ${ newPath } ` ) ;
113- await storage . move ( oldPath , newPath ) ;
114- file = await storage . get ( newPath ) ;
115- }
116- }
117- if ( ! file ) throw new HTTPError ( "not found" , 404 ) ;
118- return file ;
119- }
120-
12191export default router ;
0 commit comments