( pathname: string, image: string | File, outputDir: string, isRelative = false, currentPathname: string | null = null )
| 61 | getHash(content, 'utf8', 'sha1') |
| 62 | |
| 63 | export const moveImageToFolder = async( |
| 64 | pathname: string, |
| 65 | image: string | File, |
| 66 | outputDir: string, |
| 67 | isRelative = false, |
| 68 | currentPathname: string | null = null |
| 69 | ): Promise<string> => { |
| 70 | await window.fileUtils.ensureDir(outputDir) |
| 71 | const isPath = typeof image === 'string' |
| 72 | if (isPath) { |
| 73 | const dir = window.path.dirname(pathname) |
| 74 | const imagePath = window.path.resolve(dir, image as string) |
| 75 | const isImage = await window.fileUtils.isImageFile(imagePath) |
| 76 | if (isImage) { |
| 77 | const filename = window.path.basename(imagePath) |
| 78 | const ext = window.path.extname(imagePath) |
| 79 | const noHashPath = window.path.join(outputDir, filename) |
| 80 | if (noHashPath === imagePath) { |
| 81 | return imagePath |
| 82 | } |
| 83 | const hash = await getContentHash(imagePath) |
| 84 | const hashFilePath = window.path.join(outputDir, `${hash}${ext}`) |
| 85 | await window.fileUtils.copy(imagePath, hashFilePath) |
| 86 | return hashFilePath |
| 87 | } else { |
| 88 | return image as string |
| 89 | } |
| 90 | } else { |
| 91 | const file = image as File |
| 92 | const imagePath = window.path.join( |
| 93 | outputDir, |
| 94 | `${dayjs().format('YYYY-MM-DD-HH-mm-ss')}-${file.name}` |
| 95 | ) |
| 96 | |
| 97 | const buffer = new Uint8Array(await file.arrayBuffer()) |
| 98 | await window.fileUtils.writeFile(imagePath, buffer) |
| 99 | |
| 100 | if (isRelative && currentPathname) { |
| 101 | return window.path.relative(window.path.dirname(currentPathname), imagePath) |
| 102 | } |
| 103 | |
| 104 | return imagePath |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | export interface UploadImagePreferences { |
| 109 | currentUploader: string |
nothing calls this directly
no test coverage detected