( content: PastedContent, )
| 54 | * Store an image from pastedContents to disk. |
| 55 | */ |
| 56 | export async function storeImage( |
| 57 | content: PastedContent, |
| 58 | ): Promise<string | null> { |
| 59 | if (content.type !== 'image') { |
| 60 | return null |
| 61 | } |
| 62 | |
| 63 | try { |
| 64 | await ensureImageStoreDir() |
| 65 | const imagePath = getImagePath(content.id, content.mediaType || 'image/png') |
| 66 | const fh = await open(imagePath, 'w', 0o600) |
| 67 | try { |
| 68 | await fh.writeFile(content.content, { encoding: 'base64' }) |
| 69 | await fh.datasync() |
| 70 | } finally { |
| 71 | await fh.close() |
| 72 | } |
| 73 | evictOldestIfAtCap() |
| 74 | storedImagePaths.set(content.id, imagePath) |
| 75 | logForDebugging(`Stored image ${content.id} to ${imagePath}`) |
| 76 | return imagePath |
| 77 | } catch (error) { |
| 78 | logForDebugging(`Failed to store image: ${error}`) |
| 79 | return null |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Store all images from pastedContents to disk. |
no test coverage detected