@@ -4,7 +4,6 @@ import fs from "node:fs";
4
4
import { StrUtil , TimeUtil } from "../../lib/util" ;
5
5
import Apps from "../app" ;
6
6
import { Readable } from "node:stream" ;
7
- import { isWin } from "../../lib/env" ;
8
7
9
8
const nodePath = path
10
9
@@ -351,6 +350,47 @@ const copy = async (pathOld: string, pathNew: string, option?: { isFullPath?: bo
351
350
fs . copyFileSync ( fullPathOld , fullPathNew )
352
351
}
353
352
353
+ const hubCreate = async ( ext : string = 'bin' ) => {
354
+ return path . join (
355
+ 'hub' ,
356
+ TimeUtil . replacePattern ( '{year}' ) ,
357
+ TimeUtil . replacePattern ( '{month}' ) ,
358
+ TimeUtil . replacePattern ( '{day}' ) ,
359
+ TimeUtil . replacePattern ( '{hour}' ) ,
360
+ [
361
+ TimeUtil . replacePattern ( '{minute}' ) ,
362
+ TimeUtil . replacePattern ( '{second}' ) ,
363
+ StrUtil . randomString ( 10 ) ,
364
+ ] . join ( '_' ) + `.${ ext } `
365
+ )
366
+ }
367
+
368
+ const hubSave = async ( file : string , option ?: {
369
+ isFullPath ?: boolean ,
370
+ returnFullPath ?: boolean ,
371
+ } ) => {
372
+ option = Object . assign ( {
373
+ isFullPath : false ,
374
+ returnFullPath : false ,
375
+ } , option )
376
+ let fp = file
377
+ if ( ! option . isFullPath ) {
378
+ fp = await fullPath ( file )
379
+ }
380
+ if ( ! fs . existsSync ( fp ) ) {
381
+ throw `FileNotFound ${ fp } `
382
+ }
383
+ const fileExt = ext ( fp )
384
+ const hubFile = await hubCreate ( fileExt )
385
+ await copy ( fp , path . join ( root ( ) , hubFile ) , {
386
+ isFullPath : true ,
387
+ } )
388
+ if ( option . returnFullPath ) {
389
+ return path . join ( root ( ) , hubFile )
390
+ }
391
+ return hubFile
392
+ }
393
+
354
394
const tempRoot = async ( ) => {
355
395
await waitAppEnvReady ( )
356
396
const tempDir = path . join ( AppEnv . userData , 'temp' )
@@ -590,6 +630,7 @@ export const FileIndex = {
590
630
appendText,
591
631
download,
592
632
ext,
633
+ hubSave,
593
634
}
594
635
595
636
export default FileIndex
0 commit comments