1- import { Database , StorageBucket } from '../../Firebase' ;
2- import { GetPluginData , PluginDataProps , PluginDataTable } from './GetPluginData' ;
1+ import { join } from 'path' ;
2+ import { stat } from 'fs/promises' ;
3+ import { PluginDownloads } from '../../Database' ;
4+ import { GetPluginData , PluginDataTable } from './GetPluginData' ;
35import { GetPluginMetadata } from './GetPluginMetadata' ;
46import { RetrievePluginList } from './GetPluginList' ;
57
@@ -21,25 +23,32 @@ let cacheTimestamp = 0;
2123let inflightFetch : Promise < PluginDataTable > | null = null ;
2224
2325const fetchFreshPlugins = async ( ) : Promise < PluginDataTable > => {
24- console . log ( 'Cache miss — fetching fresh plugin data' ) ;
25-
2626 const pluginList = await RetrievePluginList ( ) ;
2727
28- const [ metadata , pluginData ] = await Promise . all ( [ GetPluginMetadata ( ) , GetPluginData ( pluginList ) ] ) ;
29- const [ files , downloadDocs ] = await Promise . all ( [ StorageBucket . getFiles ( { prefix : 'plugins/' } ) , Database . collection ( 'downloads' ) . get ( ) ] ) ;
28+ const pluginsDir = process . env . PLUGINS_DIR ;
29+ if ( ! pluginsDir ) throw new Error ( 'PLUGINS_DIR is not set' ) ;
30+
31+ const [ metadata , pluginData , downloadRows ] = await Promise . all ( [
32+ GetPluginMetadata ( ) ,
33+ GetPluginData ( pluginList ) ,
34+ Promise . resolve ( PluginDownloads . getAll ( ) ) ,
35+ ] ) ;
3036
31- // Build lookup maps once instead of scanning per-plugin
32- const fileMetadataMap = new Map < string , any > ( ) ;
37+ // Build file size map from local disk
38+ const fileMetadataMap = new Map < string , number > ( ) ;
3339 await Promise . all (
34- files [ 0 ] . map ( async ( file ) => {
35- const [ meta ] = await file . getMetadata ( ) ;
36- fileMetadataMap . set ( file . name , meta ) ;
40+ ( await import ( 'fs/promises' ) . then ( ( { readdir } ) => readdir ( pluginsDir ) . catch ( ( ) => [ ] as string [ ] ) ) ) . map ( async ( filename ) => {
41+ if ( ! filename . endsWith ( '.zip' ) ) return ;
42+ try {
43+ const s = await stat ( join ( pluginsDir , filename ) ) ;
44+ fileMetadataMap . set ( `plugins/${ filename } ` , s . size ) ;
45+ } catch { }
3746 } ) ,
3847 ) ;
3948
4049 const downloadCounts = new Map < string , number > ( ) ;
41- downloadDocs . forEach ( ( doc ) => {
42- downloadCounts . set ( doc . id , doc . data ( ) . downloadCount || 0 ) ;
50+ downloadRows . forEach ( ( row ) => {
51+ downloadCounts . set ( row . commit_id , row . download_count ) ;
4352 } ) ;
4453
4554 const metadataByCommit = new Map ( metadata . map ( ( m ) => [ m . commitId , m ] ) ) ;
@@ -53,10 +62,9 @@ const fetchFreshPlugins = async (): Promise<PluginDataTable> => {
5362
5463 const initCommitId = meta . id ;
5564 const filePath = `plugins/${ initCommitId } .zip` ;
56- const fileMetadata = fileMetadataMap . get ( filePath ) ;
57-
58- if ( fileMetadata ) {
59- plugin . downloadSize = FormatBytes ( fileMetadata . size ) ;
65+ const fileSize = fileMetadataMap . get ( filePath ) ;
66+ if ( fileSize !== undefined ) {
67+ plugin . downloadSize = FormatBytes ( fileSize ) ;
6068 }
6169
6270 plugin . downloadCount = downloadCounts . get ( initCommitId ) ?? 0 ;
0 commit comments