-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from ryoha000/feat/take.screenshot
feat: 🎸 chunk
- Loading branch information
Showing
2 changed files
with
90 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { readBinaryFile } from "@tauri-apps/api/fs"; | ||
|
||
export const useChunk = () => { | ||
let currentChunkId = 0; | ||
// chunk id は頭につける都合上8bitで表現できるようにする | ||
const chunkIdMask = 0xff; | ||
const CHUNK_SIZE = 16 * 1024; // 16KB | ||
const CHUNK_HEADER_SIZE = 2; // [chunkId: 1byte][index: 1byte] | ||
const CHUNK_DATA_SIZE = CHUNK_SIZE - CHUNK_HEADER_SIZE; | ||
const createNewChunkId = () => { | ||
return (currentChunkId + 1) & chunkIdMask; | ||
}; | ||
|
||
const createChunks = async (filePath: string) => { | ||
// ファイルをバイナリとして読み込む | ||
const data = await readBinaryFile(filePath); | ||
const lowerCasePath = filePath.toLowerCase(); | ||
// MIME タイプを推定 (ここでは ".png" の場合 "image/png" としていますが、他の形式もサポートする場合は調整が必要) | ||
const mimeType = (function () { | ||
if (lowerCasePath.endsWith(".png")) return "image/png"; | ||
if (lowerCasePath.endsWith(".jpg") || lowerCasePath.endsWith(".jpeg")) | ||
return "image/jpeg"; | ||
if (lowerCasePath.endsWith(".gif")) return "image/gif"; | ||
if (lowerCasePath.endsWith(".webp")) return "image/webp"; | ||
throw new Error("Unsupported file type"); | ||
})(); | ||
const chunkId = createNewChunkId(); | ||
|
||
const totalChunkLength = Math.ceil(data.byteLength / CHUNK_DATA_SIZE); | ||
const chunkArray: Uint8Array[] = []; | ||
for (let i = 0; i < totalChunkLength; i++) { | ||
chunkArray[i] = new Uint8Array([ | ||
chunkId, | ||
i, | ||
...data.slice( | ||
i * CHUNK_DATA_SIZE, | ||
Math.min((i + 1) * CHUNK_DATA_SIZE, data.byteLength) | ||
), | ||
]); | ||
} | ||
|
||
return [{ chunkId, mimeType, totalChunkLength }, chunkArray] as const; | ||
}; | ||
|
||
return { createChunks }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters