diff --git a/ui/src/components/editor/composables/use-attachment.ts b/ui/src/components/editor/composables/use-attachment.ts index 29e49bb9f..91283e7b1 100644 --- a/ui/src/components/editor/composables/use-attachment.ts +++ b/ui/src/components/editor/composables/use-attachment.ts @@ -1,4 +1,10 @@ +import { useGlobalInfoStore } from "@/stores/global-info"; +import { ucApiClient } from "@halo-dev/api-client"; +import { Toast } from "@halo-dev/components"; import type { AttachmentLike } from "@halo-dev/console-shared"; +import { computed, ref, type Ref } from "vue"; +import { useI18n } from "vue-i18n"; +import type { AttachmentAttr } from "../utils/attachment"; interface useAttachmentSelectReturn { onAttachmentSelect: (attachments: AttachmentLike[]) => void; @@ -24,3 +30,56 @@ export function useAttachmentSelect(): useAttachmentSelectReturn { attachmentResult, }; } + +export function useExternalAssetsTransfer( + src: Ref, + callback: (attachment: AttachmentAttr) => void +) { + const { globalInfo } = useGlobalInfoStore(); + const { t } = useI18n(); + + const isExternalAsset = computed(() => { + if (src.value?.startsWith("/")) { + return false; + } + + if (!globalInfo?.externalUrl) { + return false; + } + + return !src.value?.startsWith(globalInfo?.externalUrl); + }); + + const transferring = ref(false); + + async function handleTransfer() { + if (!src.value) { + return; + } + + transferring.value = true; + + const { data } = + await ucApiClient.storage.attachment.externalTransferAttachment1({ + ucUploadFromUrlRequest: { + url: src.value, + }, + waitForPermalink: true, + }); + + callback({ + url: data.status?.permalink, + name: data.spec.displayName, + }); + + Toast.success(t("core.common.toast.save_success")); + + transferring.value = false; + } + + return { + isExternalAsset, + transferring, + handleTransfer, + }; +} diff --git a/ui/src/components/editor/extensions/audio/AudioView.vue b/ui/src/components/editor/extensions/audio/AudioView.vue index 39a18134c..86b321747 100644 --- a/ui/src/components/editor/extensions/audio/AudioView.vue +++ b/ui/src/components/editor/extensions/audio/AudioView.vue @@ -1,10 +1,12 @@