@@ -73,7 +73,8 @@ import { StageObject } from "@/core/stage/stageObject/abstract/StageObject";
7373import { projectsAtom , store } from "@/state" ;
7474import { deserialize , serialize } from "@graphif/serializer" ;
7575import { Decoder , Encoder } from "@msgpack/msgpack" ;
76- import { Uint8ArrayReader , Uint8ArrayWriter , ZipReader , ZipWriter } from "@zip.js/zip.js" ;
76+ import { BlobReader , BlobWriter , Uint8ArrayReader , Uint8ArrayWriter , ZipReader , ZipWriter } from "@zip.js/zip.js" ;
77+ import mime from "mime" ;
7778import { URI } from "vscode-uri" ;
7879
7980if ( import . meta. hot ) {
@@ -209,16 +210,26 @@ export class Project {
209210 const fileContent = await this . fs . read ( this . uri ) ;
210211 const reader = new ZipReader ( new Uint8ArrayReader ( fileContent ) ) ;
211212 const entries = await reader . getEntries ( ) ;
213+ let serializedStageObjects : any [ ] = [ ] ;
212214 for ( const entry of entries ) {
213215 if ( entry . filename === "stage.msgpack" ) {
214216 const stageRawData = await entry . getData ! ( new Uint8ArrayWriter ( ) ) ;
215- const decoded = this . decoder . decode ( stageRawData ) as any [ ] ;
216- for ( const serializedStageObject of decoded ) {
217- const stageObject = deserialize ( serializedStageObject , this ) ;
218- this . stage . push ( stageObject ) ;
217+ serializedStageObjects = this . decoder . decode ( stageRawData ) as any [ ] ;
218+ } else if ( entry . filename . startsWith ( "attachments/" ) ) {
219+ const match = entry . filename . trim ( ) . match ( / ^ a t t a c h m e n t s \/ ( [ a - z A - Z 0 - 9 - ] + ) \. ( [ a - z A - Z 0 - 9 ] + ) $ / ) ;
220+ if ( ! match ) {
221+ console . warn ( "[Project] 附件文件名不符合规范: %s" , entry . filename ) ;
222+ continue ;
219223 }
224+ const uuid = match [ 1 ] ;
225+ const ext = match [ 2 ] ;
226+ const type = mime . getType ( ext ) || "application/octet-stream" ;
227+ const attachment = await entry . getData ! ( new BlobWriter ( type ) ) ;
228+ this . attachments . set ( uuid , attachment ) ;
220229 }
221230 }
231+ this . stage = serializedStageObjects . map ( ( it ) => deserialize ( it , this ) ) ;
232+ this . state = ProjectState . Saved ;
222233 }
223234 }
224235
@@ -303,6 +314,10 @@ export class Project {
303314 const uwriter = new Uint8ArrayWriter ( ) ;
304315 const writer = new ZipWriter ( uwriter ) ;
305316 writer . add ( "stage.msgpack" , new Uint8ArrayReader ( encodedStage ) ) ;
317+ // 添加附件
318+ for ( const [ uuid , attachment ] of this . attachments . entries ( ) ) {
319+ writer . add ( `attachments/${ uuid } .${ mime . getExtension ( attachment . type ) } ` , new BlobReader ( attachment ) ) ;
320+ }
306321 await writer . close ( ) ;
307322 const fileContent = await uwriter . getData ( ) ;
308323 await this . fs . write ( this . uri , fileContent ) ;
0 commit comments