11import { Hono } from 'hono' ;
22import type { StorageAdapter } from '@/storage/index.js' ;
33import manifestSchema from '@/schemas/manifest.js' ;
4+ import path from 'node:path' ;
45import { z } from 'zod' ;
56import * as JSZip from 'jszip' ;
67import { prisma } from '@/db.js' ;
@@ -143,7 +144,18 @@ async function formatExtensionResponse(
143144 } ;
144145}
145146
146- app . post ( '/extension/upload' , async ( c ) => {
147+ const packZipFile = ( archive : typeof JSZip . default , name : string ) => {
148+ const zip = new JSZip . default ;
149+ archive . forEach ( ( file => {
150+ const f = archive . file ( file ) ;
151+ if ( f ) {
152+ zip . file ( path . join ( name , file ) , f . async ( 'nodebuffer' ) ) ;
153+ }
154+ } ) ) ;
155+ return zip ;
156+ }
157+
158+ app . post ( '/upload' , async ( c ) => {
147159 const authHeader = c . req . header ( 'Authorization' ) ;
148160 if ( ! authHeader || authHeader !== `Bearer ${ API_SECRET } ` ) {
149161 return c . json ( { error : 'Unauthorized' } , 401 ) ;
@@ -152,7 +164,6 @@ app.post('/extension/upload', async (c) => {
152164 try {
153165 const body = await c . req . parseBody ( ) ;
154166 const file = body [ 'file' ] ;
155- console . log ( body ) ;
156167
157168 if ( ! file || ! ( file instanceof File ) ) {
158169 return c . json ( { error : 'No file uploaded' } , 400 ) ;
@@ -173,11 +184,6 @@ app.post('/extension/upload', async (c) => {
173184
174185 const arrayBuffer = await file . arrayBuffer ( ) ;
175186 const zip = await JSZip . loadAsync ( arrayBuffer ) ;
176-
177- zip . forEach ( ( path ) => {
178- console . log ( 'path' , path ) ;
179- } ) ;
180-
181187 const manifestFile = zip . file ( 'package.json' ) ;
182188
183189 if ( ! manifestFile ) {
@@ -205,9 +211,6 @@ app.post('/extension/upload', async (c) => {
205211
206212 const validatedManifest = validationResult . data ;
207213
208-
209- console . log ( validatedManifest ) ;
210-
211214 if ( ! validatedManifest . dependencies ?. [ '@vicinae/api' ] ) {
212215 return c . json (
213216 {
@@ -228,9 +231,11 @@ app.post('/extension/upload', async (c) => {
228231 // Compute checksum of the ZIP archive
229232 const fileBuffer = Buffer . from ( arrayBuffer ) ;
230233 const checksum = computeChecksum ( fileBuffer ) ;
231-
232234 const storage = c . get ( 'storage' ) ;
233- await storage . put ( storageKey , fileBuffer , {
235+ const packedZipFile = packZipFile ( zip , extensionName ) ;
236+ const packed = await packedZipFile . generateAsync ( { type : 'nodebuffer' , compression : 'DEFLATE' } ) ;
237+
238+ await storage . put ( storageKey , packed , {
234239 contentType : 'application/zip' ,
235240 contentLength : file . size ,
236241 } ) ;
0 commit comments