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