(options: { includeThumbnail?: boolean } = {})
| 396 | |
| 397 | // 备份也要用到这个 |
| 398 | async getFileContent(options: { includeThumbnail?: boolean } = {}) { |
| 399 | const includeThumbnail = options.includeThumbnail !== false; |
| 400 | const serializedStage = serialize(this.stage); |
| 401 | const encodedStage = this.encoder.encode(serializedStage); |
| 402 | const uwriter = new Uint8ArrayWriter(); |
| 403 | |
| 404 | // @zip.js/zip.js 从 ^2.7.63 升到了 ^2.8.26,需要显示指定 level: 0,防止保存的时候卡顿 |
| 405 | const writer = new ZipWriter(uwriter, { level: 0 }); |
| 406 | await writer.add("stage.msgpack", new Uint8ArrayReader(encodedStage), { level: 0 }); |
| 407 | await writer.add("tags.msgpack", new Uint8ArrayReader(this.encoder.encode(this.tags)), { level: 0 }); |
| 408 | await writer.add("reference.msgpack", new Uint8ArrayReader(this.encoder.encode(this.references)), { level: 0 }); |
| 409 | await writer.add("metadata.msgpack", new Uint8ArrayReader(this.encoder.encode(this.metadata)), { level: 0 }); |
| 410 | if (this.readme) { |
| 411 | await writer.add("README.md", new Uint8ArrayReader(new TextEncoder().encode(this.readme)), { level: 0 }); |
| 412 | } |
| 413 | // 添加附件 |
| 414 | for (const [uuid, attachment] of this.attachments.entries()) { |
| 415 | await writer.add(`attachments/${uuid}.${mime.getExtension(attachment.type)}`, new BlobReader(attachment), { |
| 416 | level: 0, |
| 417 | }); |
| 418 | } |
| 419 | // 添加缩略图 |
| 420 | if (includeThumbnail) { |
| 421 | try { |
| 422 | const thumbnailBlob = await generateThumbnail(this); |
| 423 | if (thumbnailBlob) { |
| 424 | await writer.add("thumbnail.png", new BlobReader(thumbnailBlob), { level: 0 }); |
| 425 | } |
| 426 | } catch { |
| 427 | // 缩略图生成失败不阻止保存 |
| 428 | } |
| 429 | } |
| 430 | await writer.close(); |
| 431 | |
| 432 | const fileContent = await uwriter.getData(); |
| 433 | return fileContent; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * 备份用:生成项目内容的哈希值,用于检测内容是否发生变化 |
no test coverage detected