@@ -2,7 +2,6 @@ import _ from 'lodash';
22import { diffConvertYaml } from '@serverless-devs/diff' ;
33import inquirer from 'inquirer' ;
44import fs from 'fs' ;
5- import os from 'os' ;
65import assert from 'assert' ;
76import path from 'path' ;
87import { yellow } from 'chalk' ;
@@ -19,10 +18,15 @@ import FC, { GetApiType } from '../../../resources/fc';
1918import VPC_NAS from '../../../resources/vpc-nas' ;
2019import Base from './base' ;
2120import { ICredentials } from '@serverless-devs/component-interface' ;
22- import { calculateCRC64 , getFileSize , parseAutoConfig , checkFcDir } from '../../../utils' ;
21+ import {
22+ calculateCRC64 ,
23+ getFileSize ,
24+ parseAutoConfig ,
25+ checkFcDir ,
26+ _downloadFromUrl ,
27+ } from '../../../utils' ;
2328import OSS from '../../../resources/oss' ;
2429import { setNodeModulesBinPermissions } from '../../../resources/fc/impl/utils' ;
25- import downloads from '@serverless-devs/downloads' ;
2630
2731type IType = 'code' | 'config' | boolean ;
2832interface IOpts {
@@ -280,11 +284,11 @@ export default class Service extends Base {
280284 }
281285
282286 let zipPath : string ;
283- let downloadedTempDir = '' ;
287+ let downloadedTempFile = '' ;
284288 // 处理不同类型的 codeUri
285289 if ( codeUri . startsWith ( 'http://' ) || codeUri . startsWith ( 'https://' ) ) {
286- zipPath = await this . _downloadFromUrl ( codeUri ) ;
287- downloadedTempDir = path . dirname ( zipPath ) ;
290+ zipPath = await _downloadFromUrl ( codeUri ) ;
291+ downloadedTempFile = zipPath ;
288292 } else {
289293 zipPath = path . isAbsolute ( codeUri ) ? codeUri : path . join ( this . inputs . baseDir , codeUri ) ;
290294 }
@@ -321,6 +325,14 @@ export default class Service extends Base {
321325 logger . debug (
322326 yellow ( `skip uploadCode because code is no changed, codeChecksum=${ crc64Value } ` ) ,
323327 ) ;
328+ if ( downloadedTempFile ) {
329+ try {
330+ logger . debug ( `Removing temp download dir: ${ downloadedTempFile } ` ) ;
331+ fs . rmSync ( downloadedTempFile , { recursive : true , force : true } ) ;
332+ } catch ( ex ) {
333+ logger . debug ( `Unable to remove temp download dir: ${ downloadedTempFile } ` ) ;
334+ }
335+ }
324336 return false ;
325337 } else {
326338 logger . debug ( `\x1b[33mcodeChecksum from ${ this . codeChecksum } to ${ crc64Value } \x1b[0m` ) ;
@@ -338,58 +350,18 @@ export default class Service extends Base {
338350 }
339351 }
340352
341- if ( downloadedTempDir ) {
353+ if ( downloadedTempFile ) {
342354 try {
343- logger . debug ( `Removing temp download dir: ${ downloadedTempDir } ` ) ;
344- fs . rmSync ( downloadedTempDir , { recursive : true , force : true } ) ;
355+ logger . debug ( `Removing temp download dir: ${ downloadedTempFile } ` ) ;
356+ fs . rmSync ( downloadedTempFile , { recursive : true , force : true } ) ;
345357 } catch ( ex ) {
346- logger . debug ( `Unable to remove temp download dir: ${ downloadedTempDir } ` ) ;
358+ logger . debug ( `Unable to remove temp download dir: ${ downloadedTempFile } ` ) ;
347359 }
348360 }
349361
350362 return true ;
351363 }
352364
353- /**
354- * 从URL下载文件到本地临时目录
355- */
356- private async _downloadFromUrl ( url : string ) : Promise < string > {
357- logger . info ( `Downloading code from URL: ${ url } ` ) ;
358-
359- // 创建临时目录
360- const tempDir = path . join ( os . tmpdir ( ) , 'fc_code_download' ) ;
361- let downloadPath : string ;
362-
363- try {
364- // 从URL获取文件名
365- const urlPath = new URL ( url ) . pathname ;
366- const parsedPathName = path . parse ( urlPath ) . name ;
367- const filename = path . basename ( urlPath ) || `downloaded_code_${ Date . now ( ) } ` ;
368- downloadPath = path . join ( tempDir , filename ) ;
369-
370- await downloads ( url , {
371- dest : tempDir ,
372- filename : parsedPathName ,
373- extract : false ,
374- } ) ;
375-
376- logger . debug ( `Downloaded file to: ${ downloadPath } ` ) ;
377-
378- // 返回下载文件路径,由主流程决定是否需要压缩
379- return downloadPath ;
380- } catch ( error ) {
381- // 如果下载失败,清理临时目录
382- try {
383- fs . rmSync ( tempDir , { recursive : true , force : true } ) ;
384- logger . debug ( `Cleaned up temporary directory after error: ${ tempDir } ` ) ;
385- } catch ( cleanupError ) {
386- logger . debug ( `Failed to clean up temporary directory: ${ cleanupError . message } ` ) ;
387- }
388-
389- throw new Error ( `Failed to download code from URL: ${ error . message } ` ) ;
390- }
391- }
392-
393365 /**
394366 * 生成 auto 资源,非 FC 资源,主要指 vpc、nas、log、role(oss mount 挂载点才有)
395367 */
0 commit comments