MCPcopy
hub / github.com/marktext/marktext / moveImageToFolder

Function moveImageToFolder

packages/desktop/src/renderer/src/util/fileSystem.ts:63–106  ·  view source on GitHub ↗
(
  pathname: string,
  image: string | File,
  outputDir: string,
  isRelative = false,
  currentPathname: string | null = null
)

Source from the content-addressed store, hash-verified

61 getHash(content, 'utf8', 'sha1')
62
63export 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
108export interface UploadImagePreferences {
109 currentUploader: string

Callers

nothing calls this directly

Calls 12

getContentHashFunction · 0.85
ensureDirMethod · 0.80
dirnameMethod · 0.80
resolveMethod · 0.80
isImageFileMethod · 0.80
basenameMethod · 0.80
extnameMethod · 0.80
joinMethod · 0.80
writeFileMethod · 0.80
relativeMethod · 0.80
copyMethod · 0.65
formatMethod · 0.65

Tested by

no test coverage detected