@@ -4,7 +4,6 @@ import fs from "node:fs";
44import { StrUtil , TimeUtil } from "../../lib/util" ;
55import Apps from "../app" ;
66import { Readable } from "node:stream" ;
7- import { isWin } from "../../lib/env" ;
87
98const nodePath = path
109
@@ -351,6 +350,47 @@ const copy = async (pathOld: string, pathNew: string, option?: { isFullPath?: bo
351350 fs . copyFileSync ( fullPathOld , fullPathNew )
352351}
353352
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+
354394const tempRoot = async ( ) => {
355395 await waitAppEnvReady ( )
356396 const tempDir = path . join ( AppEnv . userData , 'temp' )
@@ -590,6 +630,7 @@ export const FileIndex = {
590630 appendText,
591631 download,
592632 ext,
633+ hubSave,
593634}
594635
595636export default FileIndex
0 commit comments