@@ -24,6 +24,8 @@ import { HTTPError } from "lambert-server";
2424import crypto from "crypto" ;
2525import { multer } from "../util/multer" ;
2626import { cache } from "../util/cache" ;
27+ import { FileStorage } from "@spacebar/cdn*" ;
28+ import fs from "fs/promises" ;
2729
2830// TODO: check premium and animated pfp are allowed in the config
2931// TODO: generate different sizes of icon
@@ -103,7 +105,25 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => {
103105
104106async function getOrMoveFile ( newPath : string , oldPath : string ) {
105107 let file = await storage . get ( newPath ) ;
106- if ( ! file ) {
108+ if ( file ) {
109+ if ( ! ( await storage . isFile ( newPath ) ) ) {
110+ console . log ( `[CDN] Migrating sticker from subdirectory+fallback to direct path for ${ newPath } ` ) ;
111+ // noinspection SuspiciousTypeOfGuard -- not sure whats up with this
112+ if ( storage instanceof FileStorage ) {
113+ const files = await fs . readdir ( storage . getFsPath ( newPath ) ) ;
114+ if ( files . length === 1 ) {
115+ const oldFilePath = storage . getFsPath ( `${ newPath } /${ files [ 0 ] } ` ) ;
116+ const newFilePath = storage . getFsPath ( newPath ) ;
117+ await fs . rename ( oldFilePath , newFilePath + ".tmp" ) ;
118+ await fs . rmdir ( storage . getFsPath ( newPath ) ) ;
119+ await fs . rename ( newFilePath + ".tmp" , newFilePath ) ;
120+ file = await storage . get ( newPath ) ;
121+ } else console . log ( `[CDN] Warning: not migrating sticker ${ newPath } , as there are multiple files in the old directory` ) ;
122+ } else {
123+ console . log ( "[CDN] Warning: no migration for s3 storage stickers, as it is not a filesystem" ) ;
124+ }
125+ }
126+ } else {
107127 if ( await storage . exists ( oldPath ) ) {
108128 console . log ( `[${ pathPrefix } ] found file at old path ${ oldPath } , moving to new path ${ newPath } ` ) ;
109129 await storage . move ( oldPath , newPath ) ;
0 commit comments