(
key: string,
content: ArrayBuffer,
mtime: number,
ctime: number
)
| 622 | } |
| 623 | |
| 624 | async writeFile( |
| 625 | key: string, |
| 626 | content: ArrayBuffer, |
| 627 | mtime: number, |
| 628 | ctime: number |
| 629 | ): Promise<Entity> { |
| 630 | if (key.endsWith("/")) { |
| 631 | throw Error(`should not call writeFile on ${key}`); |
| 632 | } |
| 633 | |
| 634 | await this._init(); |
| 635 | |
| 636 | const prevCachedEntity: BoxEntity | undefined = this.keyToBoxEntity[key]; |
| 637 | const prevFileID: string | undefined = prevCachedEntity?.id; |
| 638 | |
| 639 | const contentType = |
| 640 | mime.contentType(mime.lookup(key) || DEFAULT_CONTENT_TYPE) || |
| 641 | DEFAULT_CONTENT_TYPE; |
| 642 | |
| 643 | let parentID: string | undefined = undefined; |
| 644 | let parentFolderPath: string | undefined = undefined; |
| 645 | |
| 646 | // "xxx" => [] |
| 647 | // "xxx/yyy/zzz.md" => ["xxx", "xxx/yyy"] |
| 648 | const folderLevels = getFolderLevels(key); |
| 649 | if (folderLevels.length === 0) { |
| 650 | // root |
| 651 | parentID = this.baseDirID; |
| 652 | parentFolderPath = ""; |
| 653 | } else { |
| 654 | parentFolderPath = `${folderLevels[folderLevels.length - 1]}/`; |
| 655 | if (!(parentFolderPath in this.keyToBoxEntity)) { |
| 656 | throw Error( |
| 657 | `parent of ${key}: ${parentFolderPath} is not created before??` |
| 658 | ); |
| 659 | } |
| 660 | parentID = this.keyToBoxEntity[parentFolderPath].id; |
| 661 | } |
| 662 | |
| 663 | const fileItself = key.split("/").pop()!; |
| 664 | |
| 665 | const BIG_FILE_THRESHOLD = 20000000; // box api hard coded... |
| 666 | if (content.byteLength <= BIG_FILE_THRESHOLD) { |
| 667 | const formData = new FormData(); |
| 668 | const attributes = { |
| 669 | name: fileItself, |
| 670 | parent: { id: parentID }, |
| 671 | content_created_at: unixTimeToStr(ctime), |
| 672 | content_modified_at: unixTimeToStr(mtime), |
| 673 | }; |
| 674 | formData.append("attributes", JSON.stringify(attributes)); |
| 675 | formData.append("file", new Blob([content], { type: contentType })); |
| 676 | |
| 677 | let url = ""; |
| 678 | if (prevFileID === undefined) { |
| 679 | // create new file |
| 680 | // https://developer.box.com/reference/post-files-content/ |
| 681 | url = `https://upload.box.com/api/2.0/files/content`; |
no test coverage detected