Line data Source code
1 : import { useI18n } from 'vue-i18n'
2 :
3 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
4 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
5 : import NodeOutputsExportDialog from '@/platform/assets/components/NodeOutputsExportDialog.vue'
6 : import { useAssetDownload } from '@/platform/assets/composables/useAssetDownload'
7 : import { useAssetZipExport } from '@/platform/assets/composables/useAssetZipExport'
8 : import type { DownloadableOutput } from '@/platform/assets/utils/outputExportUtil'
9 : import {
10 : buildOutputsExportRequest,
11 : isDownloadableOutput,
12 : outputFileUrl
13 : } from '@/platform/assets/utils/outputExportUtil'
14 : import { useDialogService } from '@/services/dialogService'
15 : import { useDialogStore } from '@/stores/dialogStore'
16 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
17 : import { hdrPreviewAssetUrl, isHdrImageFilename } from '@/utils/hdrFormatUtil'
18 : import { getGridThumbnailUrl } from '@/utils/imageUtil'
19 :
20 2355 : const EXPORT_DIALOG_KEY = 'node-outputs-export'
21 :
22 4889 : export function useNodeOutputsExport() {
23 4889 : const { t } = useI18n()
24 4889 : const { flags } = useFeatureFlags()
25 4889 : const nodeOutputStore = useNodeOutputStore()
26 4889 : const dialogService = useDialogService()
27 4889 : const dialogStore = useDialogStore()
28 4889 : const { downloadFiles } = useAssetDownload()
29 4889 : const { startZipExport } = useAssetZipExport()
30 :
31 2319 : function getDownloadableOutputs(node: LGraphNode): DownloadableOutput[] {
32 2319 : return (nodeOutputStore.getNodeOutputs(node)?.images ?? []).filter(
33 : isDownloadableOutput
34 : )
35 : }
36 :
37 2319 : function hasMultipleOutputs(node: LGraphNode): boolean {
38 2319 : return getDownloadableOutputs(node).length > 1
39 : }
40 :
41 0 : function thumbnailUrl(output: DownloadableOutput): string | undefined {
42 0 : if (!isHdrImageFilename(output.filename)) {
43 0 : return getGridThumbnailUrl(outputFileUrl(output))
44 : }
45 0 : return output.preview_id ? hdrPreviewAssetUrl(output.preview_id) : undefined
46 : }
47 :
48 0 : function closeExportDialog() {
49 0 : dialogStore.closeDialog({ key: EXPORT_DIALOG_KEY })
50 : }
51 :
52 0 : function showOutputsExportDialog(node: LGraphNode): void {
53 0 : const outputs = getDownloadableOutputs(node)
54 0 : if (outputs.length === 0) return
55 :
56 0 : dialogService.showSmallLayoutDialog({
57 : key: EXPORT_DIALOG_KEY,
58 : title: t('nodeOutputsExport.title'),
59 : component: NodeOutputsExportDialog,
60 : props: {
61 : items: outputs.map((output) => ({
62 : name: output.filename,
63 : thumbnailUrl: thumbnailUrl(output)
64 : })),
65 : onCancel: closeExportDialog,
66 : onExport: (selectedIndices: number[]) => {
67 0 : closeExportDialog()
68 0 : void exportOutputs(selectedIndices.map((index) => outputs[index]))
69 : }
70 : }
71 : })
72 : }
73 :
74 0 : async function exportOutputs(outputs: DownloadableOutput[]): Promise<void> {
75 0 : const request =
76 : outputs.length > 1 && flags.assetsEnabled
77 : ? buildOutputsExportRequest(outputs)
78 : : undefined
79 0 : if (request) {
80 0 : await startZipExport(request, outputs.length)
81 0 : return
82 : }
83 :
84 0 : await downloadFiles(
85 : outputs.map((output) => ({
86 : mode: 'direct',
87 : url: outputFileUrl(output),
88 : filename: output.filename
89 : }))
90 : )
91 : }
92 :
93 4889 : return { hasMultipleOutputs, showOutputsExportDialog }
94 : }
|