@@ -2,6 +2,8 @@ import _ from 'lodash';
22import { diffConvertYaml } from '@serverless-devs/diff' ;
33import inquirer from 'inquirer' ;
44import fs from 'fs' ;
5+ import os from 'os' ;
6+ import axios from 'axios' ;
57import assert from 'assert' ;
68import path from 'path' ;
79import { yellow } from 'chalk' ;
@@ -277,9 +279,13 @@ export default class Service extends Base {
277279 return true ;
278280 }
279281
280- let zipPath : string = path . isAbsolute ( codeUri )
281- ? codeUri
282- : path . join ( this . inputs . baseDir , codeUri ) ;
282+ let zipPath : string ;
283+ // 处理不同类型的 codeUri
284+ if ( codeUri . startsWith ( 'http://' ) || codeUri . startsWith ( 'https://' ) ) {
285+ zipPath = await this . _downloadFromUrl ( codeUri ) ;
286+ } else {
287+ zipPath = path . isAbsolute ( codeUri ) ? codeUri : path . join ( this . inputs . baseDir , codeUri ) ;
288+ }
283289 logger . debug ( `Code path absolute path: ${ zipPath } ` ) ;
284290
285291 const needZip = this . _assertNeedZip ( codeUri ) ;
@@ -313,6 +319,15 @@ export default class Service extends Base {
313319 logger . debug (
314320 yellow ( `skip uploadCode because code is no changed, codeChecksum=${ crc64Value } ` ) ,
315321 ) ;
322+ // 清理临时文件
323+ if ( generateZipFilePath ) {
324+ try {
325+ fs . rmSync ( generateZipFilePath ) ;
326+ } catch ( ex ) {
327+ logger . debug ( `Unable to remove zip file: ${ zipPath } ` ) ;
328+ }
329+ }
330+
316331 return false ;
317332 } else {
318333 logger . debug ( `\x1b[33mcodeChecksum from ${ this . codeChecksum } to ${ crc64Value } \x1b[0m` ) ;
@@ -333,6 +348,46 @@ export default class Service extends Base {
333348 return true ;
334349 }
335350
351+ /**
352+ * 从URL下载文件到本地临时目录
353+ */
354+ private async _downloadFromUrl ( url : string ) : Promise < string > {
355+ logger . warn ( `Downloading code from URL: ${ url } ` ) ;
356+
357+ // 创建临时目录
358+ const tempDir = path . join ( os . tmpdir ( ) , 'fc_code_download' , Date . now ( ) . toString ( ) ) ;
359+ fs . mkdirSync ( tempDir , { recursive : true } ) ;
360+
361+ try {
362+ // 从URL获取文件名
363+ const urlPath = new URL ( url ) . pathname ;
364+ const filename = path . basename ( urlPath ) || `downloaded_code_${ Date . now ( ) } ` ;
365+ const downloadPath = path . join ( tempDir , filename ) ;
366+
367+ // 下载文件
368+ const response = await axios ( {
369+ method : 'GET' ,
370+ url,
371+ responseType : 'stream' ,
372+ } ) ;
373+
374+ const writer = fs . createWriteStream ( downloadPath ) ;
375+ response . data . pipe ( writer ) ;
376+
377+ await new Promise < void > ( ( resolve , reject ) => {
378+ writer . on ( 'finish' , resolve ) ;
379+ writer . on ( 'error' , reject ) ;
380+ } ) ;
381+
382+ logger . debug ( `Downloaded file to: ${ downloadPath } ` ) ;
383+
384+ // 返回下载文件路径,由主流程决定是否需要压缩
385+ return downloadPath ;
386+ } catch ( error ) {
387+ throw new Error ( `Failed to download code from URL: ${ error . message } ` ) ;
388+ }
389+ }
390+
336391 /**
337392 * 生成 auto 资源,非 FC 资源,主要指 vpc、nas、log、role(oss mount 挂载点才有)
338393 */
0 commit comments