Fix pasting image that was copied from browser (#2630)

This commit is contained in:
bymyself
2025-02-19 08:27:58 -07:00
committed by GitHub
parent 0d6bc669f5
commit 40fa1d37bc
2 changed files with 40 additions and 3 deletions

View File

@@ -3,15 +3,14 @@ import { defineStore } from 'pinia'
import { api } from '@/scripts/api'
import { ExecutedWsMessage, ResultItem } from '@/types/apiTypes'
import { parseFilePath } from '@/utils/formatUtil'
const toOutputs = (
filenames: string[],
type: string
): ExecutedWsMessage['output'] => {
return {
images: filenames.map((image) => {
return { filename: image, subfolder: '', type }
})
images: filenames.map((image) => ({ type, ...parseFilePath(image) }))
}
}

View File

@@ -232,3 +232,41 @@ export function createAnnotatedPath(
return `${createPath(item, subfolder)}${createAnnotation(rootFolder)}`
return `${createPath(item.filename ?? '', item.subfolder)}${createAnnotation(item.type)}`
}
/**
* Parses a filepath into its filename and subfolder components.
*
* @example
* parseFilePath('folder/file.txt') // → { filename: 'file.txt', subfolder: 'folder' }
* parseFilePath('/folder/file.txt') // → { filename: 'file.txt', subfolder: 'folder' }
* parseFilePath('file.txt') // → { filename: 'file.txt', subfolder: '' }
* parseFilePath('folder//file.txt') // → { filename: 'file.txt', subfolder: 'folder' }
*
* @param filepath The filepath to parse
* @returns Object containing filename and subfolder
*/
export function parseFilePath(filepath: string): {
filename: string
subfolder: string
} {
if (!filepath?.trim()) return { filename: '', subfolder: '' }
const normalizedPath = filepath
.replace(/[\\/]+/g, '/') // Normalize path separators
.replace(/^\//, '') // Remove leading slash
.replace(/\/$/, '') // Remove trailing slash
const lastSlashIndex = normalizedPath.lastIndexOf('/')
if (lastSlashIndex === -1) {
return {
filename: normalizedPath,
subfolder: ''
}
}
return {
filename: normalizedPath.slice(lastSlashIndex + 1),
subfolder: normalizedPath.slice(0, lastSlashIndex)
}
}