Skip to content

Commit 3211875

Browse files
[refactor] Refactor and type image upload options (#4185)
1 parent a6bd04f commit 3211875

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

src/composables/node/useNodeImageUpload.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@ import type { LGraphNode } from '@comfyorg/litegraph'
33
import { useNodeDragAndDrop } from '@/composables/node/useNodeDragAndDrop'
44
import { useNodeFileInput } from '@/composables/node/useNodeFileInput'
55
import { useNodePaste } from '@/composables/node/useNodePaste'
6+
import type { ResultItemType } from '@/schemas/apiSchema'
67
import { api } from '@/scripts/api'
78
import { useToastStore } from '@/stores/toastStore'
89

910
const PASTED_IMAGE_EXPIRY_MS = 2000
1011

11-
const uploadFile = async (file: File, isPasted: boolean) => {
12+
interface ImageUploadFormFields {
13+
/**
14+
* The folder to upload the file to.
15+
* @example 'input', 'output', 'temp'
16+
*/
17+
type: ResultItemType
18+
}
19+
20+
const uploadFile = async (
21+
file: File,
22+
isPasted: boolean,
23+
formFields: Partial<ImageUploadFormFields> = {}
24+
) => {
1225
const body = new FormData()
1326
body.append('image', file)
1427
if (isPasted) body.append('subfolder', 'pasted')
28+
if (formFields.type) body.append('type', formFields.type)
1529

1630
const resp = await api.fetchApi('/upload/image', {
1731
method: 'POST',
@@ -53,7 +67,7 @@ export const useNodeImageUpload = (
5367

5468
const handleUpload = async (file: File) => {
5569
try {
56-
const path = await uploadFile(file, isPastedFile(file))
70+
const path = await uploadFile(file, isPastedFile(file), {})
5771
if (!path) return
5872
return path
5973
} catch (error) {

src/composables/widgets/useImageUploadWidget.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useNodeImageUpload } from '@/composables/node/useNodeImageUpload'
66
import { useValueTransform } from '@/composables/useValueTransform'
77
import { t } from '@/i18n'
88
import type { ResultItem } from '@/schemas/apiSchema'
9-
import type { InputSpec } from '@/schemas/nodeDefSchema'
9+
import type { ComboInputOptions, InputSpec } from '@/schemas/nodeDefSchema'
1010
import type { ComfyWidgetConstructor } from '@/scripts/widgets'
1111
import { useNodeOutputStore } from '@/stores/imagePreviewStore'
1212
import { createAnnotatedPath } from '@/utils/formatUtil'
@@ -33,7 +33,7 @@ export const useImageUploadWidget = () => {
3333
inputName: string,
3434
inputData: InputSpec
3535
) => {
36-
const inputOptions = inputData[1] ?? {}
36+
const inputOptions = (inputData[1] ?? {}) as ComboInputOptions
3737
const { imageInputName, allow_batch, image_folder = 'input' } = inputOptions
3838
const nodeOutputStore = useNodeOutputStore()
3939

@@ -43,11 +43,9 @@ export const useImageUploadWidget = () => {
4343
const { showPreview } = isVideo ? useNodeVideo(node) : useNodeImage(node)
4444

4545
const fileFilter = isVideo ? isVideoFile : isImageFile
46-
// @ts-expect-error InputSpec is not typed correctly
47-
const fileComboWidget = findFileComboWidget(node, imageInputName)
46+
const fileComboWidget = findFileComboWidget(node, imageInputName ?? '')
4847
const initialFile = `${fileComboWidget.value}`
4948
const formatPath = (value: InternalFile) =>
50-
// @ts-expect-error InputSpec is not typed correctly
5149
createAnnotatedPath(value, { rootFolder: image_folder })
5250

5351
const transform = (internalValue: InternalValue): ExposedValue => {
@@ -67,7 +65,6 @@ export const useImageUploadWidget = () => {
6765

6866
// Setup file upload handling
6967
const { openFileSelection } = useNodeImageUpload(node, {
70-
// @ts-expect-error InputSpec is not typed correctly
7168
allow_batch,
7269
fileFilter,
7370
accept,

src/extensions/core/uploadAudio.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import { useNodeDragAndDrop } from '@/composables/node/useNodeDragAndDrop'
55
import { useNodeFileInput } from '@/composables/node/useNodeFileInput'
66
import { useNodePaste } from '@/composables/node/useNodePaste'
77
import { t } from '@/i18n'
8+
import type { ResultItemType } from '@/schemas/apiSchema'
89
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'
910
import type { DOMWidget } from '@/scripts/domWidget'
1011
import { useToastStore } from '@/stores/toastStore'
1112

1213
import { api } from '../../scripts/api'
1314
import { app } from '../../scripts/app'
1415

15-
type FolderType = 'input' | 'output' | 'temp'
16-
1716
function splitFilePath(path: string): [string, string] {
1817
const folder_separator = path.lastIndexOf('/')
1918
if (folder_separator === -1) {
@@ -28,7 +27,7 @@ function splitFilePath(path: string): [string, string] {
2827
function getResourceURL(
2928
subfolder: string,
3029
filename: string,
31-
type: FolderType = 'input'
30+
type: ResultItemType = 'input'
3231
): string {
3332
const params = [
3433
'filename=' + encodeURIComponent(filename),

src/schemas/apiSchema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import { zKeybinding } from '@/schemas/keyBindingSchema'
88
import { NodeBadgeMode } from '@/types/nodeSource'
99
import { LinkReleaseTriggerAction } from '@/types/searchBoxTypes'
1010

11+
export const resultItemType = z.enum(['input', 'output', 'temp'])
1112
const zNodeType = z.string()
1213
const zQueueIndex = z.number()
1314
const zPromptId = z.string()
1415
const zResultItem = z.object({
1516
filename: z.string().optional(),
1617
subfolder: z.string().optional(),
17-
type: z.string().optional()
18+
type: resultItemType.optional()
1819
})
20+
export type ResultItemType = z.infer<typeof resultItemType>
1921
export type ResultItem = z.infer<typeof zResultItem>
2022
const zOutputs = z
2123
.object({

src/schemas/nodeDefSchema.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { z } from 'zod'
22
import { fromZodError } from 'zod-validation-error'
33

4+
import { resultItemType } from './apiSchema'
5+
46
const zComboOption = z.union([z.string(), z.number()])
57
const zRemoteWidgetConfig = z.object({
68
route: z.string().url().or(z.string().startsWith('/')),
@@ -72,14 +74,19 @@ export const zStringInputOptions = zBaseInputOptions.extend({
7274
export const zComboInputOptions = zBaseInputOptions.extend({
7375
control_after_generate: z.boolean().optional(),
7476
image_upload: z.boolean().optional(),
75-
image_folder: z.enum(['input', 'output', 'temp']).optional(),
77+
image_folder: resultItemType.optional(),
7678
allow_batch: z.boolean().optional(),
7779
video_upload: z.boolean().optional(),
7880
animated_image_upload: z.boolean().optional(),
7981
options: z.array(zComboOption).optional(),
8082
remote: zRemoteWidgetConfig.optional(),
8183
/** Whether the widget is a multi-select widget. */
82-
multi_select: zMultiSelectOption.optional()
84+
multi_select: zMultiSelectOption.optional(),
85+
/**
86+
* The name of the image upload input (filename combo) if it exists
87+
* @example 'image'
88+
*/
89+
imageInputName: z.string().optional()
8390
})
8491

8592
const zIntInputSpec = z.tuple([z.literal('INT'), zIntInputOptions.optional()])

src/stores/imagePreviewStore.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { LGraphNode } from '@comfyorg/litegraph'
22
import { defineStore } from 'pinia'
33

4-
import { ExecutedWsMessage, ResultItem } from '@/schemas/apiSchema'
4+
import {
5+
ExecutedWsMessage,
6+
ResultItem,
7+
ResultItemType
8+
} from '@/schemas/apiSchema'
59
import { api } from '@/scripts/api'
610
import { app } from '@/scripts/app'
711
import { parseFilePath } from '@/utils/formatUtil'
812
import { isVideoNode } from '@/utils/litegraphUtil'
913

1014
const createOutputs = (
1115
filenames: string[],
12-
type: string,
16+
type: ResultItemType,
1317
isAnimated: boolean
1418
): ExecutedWsMessage['output'] => {
1519
return {
@@ -88,7 +92,7 @@ export const useNodeOutputStore = defineStore('nodeOutput', () => {
8892
{
8993
folder = 'input',
9094
isAnimated = false
91-
}: { folder?: string; isAnimated?: boolean } = {}
95+
}: { folder?: ResultItemType; isAnimated?: boolean } = {}
9296
) {
9397
if (!filenames || !node) return
9498

0 commit comments

Comments
 (0)