|
| 1 | +import { computed, ref } from 'vue' |
| 2 | +import { useI18n } from 'vue-i18n' |
| 3 | +import { FileService } from '@/services' |
| 4 | +import { useAlertStore } from '@/stores/alertStore' |
| 5 | +import { useConfigStore } from '@/stores/configStore' |
| 6 | +import type { LocalShareItem, LocalShareResult } from '@/types' |
| 7 | +import { copyToClipboard } from '@/utils/clipboard' |
| 8 | +import { getErrorMessage } from '@/utils/common' |
| 9 | +import { buildRetrieveUrl } from '@/utils/share-url' |
| 10 | + |
| 11 | +export function useLocalShare() { |
| 12 | + const { t } = useI18n() |
| 13 | + const alertStore = useAlertStore() |
| 14 | + const configStore = useConfigStore() |
| 15 | + |
| 16 | + const currentPath = ref('') |
| 17 | + const items = ref<LocalShareItem[]>([]) |
| 18 | + const truncated = ref(false) |
| 19 | + const isLoading = ref(false) |
| 20 | + const isSharing = ref(false) |
| 21 | + const deletingPath = ref('') |
| 22 | + const showShareModal = ref(false) |
| 23 | + const sharingItem = ref<LocalShareItem | null>(null) |
| 24 | + const shareResult = ref<LocalShareResult | null>(null) |
| 25 | + const expireStyle = ref('day') |
| 26 | + const expireValue = ref(1) |
| 27 | + |
| 28 | + const expireOptions = computed(() => { |
| 29 | + const styles = configStore.config.expire_style?.length |
| 30 | + ? configStore.config.expire_style |
| 31 | + : ['day', 'hour', 'minute', 'forever', 'count'] |
| 32 | + return styles.map((value) => ({ |
| 33 | + value, |
| 34 | + label: t(`send.time.${value}`) |
| 35 | + })) |
| 36 | + }) |
| 37 | + |
| 38 | + const breadcrumbs = computed(() => { |
| 39 | + if (!currentPath.value) return [] |
| 40 | + const parts = currentPath.value.split('/').filter(Boolean) |
| 41 | + return parts.map((name, index) => ({ |
| 42 | + name, |
| 43 | + path: parts.slice(0, index + 1).join('/') |
| 44 | + })) |
| 45 | + }) |
| 46 | + |
| 47 | + const shareModalTitle = computed(() => |
| 48 | + shareResult.value ? t('admin.localShare.resultTitle') : t('admin.localShare.shareTitle') |
| 49 | + ) |
| 50 | + |
| 51 | + const loadDirectory = async (path: string) => { |
| 52 | + isLoading.value = true |
| 53 | + try { |
| 54 | + const response = await FileService.listLocalFiles(path) |
| 55 | + if (response.code !== 200 || !response.detail) { |
| 56 | + throw new Error(t('admin.localShare.loadFailed')) |
| 57 | + } |
| 58 | + currentPath.value = response.detail.path || '' |
| 59 | + items.value = response.detail.items || [] |
| 60 | + truncated.value = Boolean(response.detail.truncated) |
| 61 | + } catch (error) { |
| 62 | + alertStore.showAlert(getErrorMessage(error, t('admin.localShare.loadFailed')), 'error') |
| 63 | + } finally { |
| 64 | + isLoading.value = false |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const openShare = (item: LocalShareItem) => { |
| 69 | + sharingItem.value = item |
| 70 | + shareResult.value = null |
| 71 | + expireStyle.value = expireOptions.value[0]?.value || 'day' |
| 72 | + expireValue.value = 1 |
| 73 | + showShareModal.value = true |
| 74 | + } |
| 75 | + |
| 76 | + const closeShareModal = () => { |
| 77 | + showShareModal.value = false |
| 78 | + sharingItem.value = null |
| 79 | + shareResult.value = null |
| 80 | + } |
| 81 | + |
| 82 | + const confirmShare = async () => { |
| 83 | + if (!sharingItem.value) return |
| 84 | + isSharing.value = true |
| 85 | + try { |
| 86 | + const response = await FileService.shareLocalFile({ |
| 87 | + filename: sharingItem.value.path, |
| 88 | + expire_value: expireStyle.value === 'forever' ? 1 : Math.max(1, Number(expireValue.value) || 1), |
| 89 | + expire_style: expireStyle.value |
| 90 | + }) |
| 91 | + if (response.code !== 200 || !response.detail?.code) { |
| 92 | + throw new Error(t('admin.localShare.shareFailed')) |
| 93 | + } |
| 94 | + shareResult.value = response.detail |
| 95 | + alertStore.showAlert( |
| 96 | + t('admin.localShare.shareSuccess', { code: response.detail.code }), |
| 97 | + 'success' |
| 98 | + ) |
| 99 | + } catch (error) { |
| 100 | + alertStore.showAlert(getErrorMessage(error, t('admin.localShare.shareFailed')), 'error') |
| 101 | + } finally { |
| 102 | + isSharing.value = false |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + const copyCode = async () => { |
| 107 | + if (!shareResult.value) return |
| 108 | + await copyToClipboard(shareResult.value.code, { |
| 109 | + notify: (message, type) => alertStore.showAlert(message, type) |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + const copyLink = async () => { |
| 114 | + if (!shareResult.value) return |
| 115 | + await copyToClipboard(buildRetrieveUrl(shareResult.value.code), { |
| 116 | + notify: (message, type) => alertStore.showAlert(message, type) |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + const removeFile = async (item: LocalShareItem) => { |
| 121 | + if (!window.confirm(t('admin.localShare.deleteConfirm', { name: item.path }))) { |
| 122 | + return |
| 123 | + } |
| 124 | + deletingPath.value = item.path |
| 125 | + try { |
| 126 | + const response = await FileService.deleteLocalFile(item.path) |
| 127 | + if (response.code !== 200) { |
| 128 | + throw new Error(t('admin.localShare.deleteFailed')) |
| 129 | + } |
| 130 | + alertStore.showAlert(t('admin.localShare.deleteSuccess'), 'success') |
| 131 | + await loadDirectory(currentPath.value) |
| 132 | + } catch (error) { |
| 133 | + alertStore.showAlert(getErrorMessage(error, t('admin.localShare.deleteFailed')), 'error') |
| 134 | + } finally { |
| 135 | + deletingPath.value = '' |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return { |
| 140 | + breadcrumbs, |
| 141 | + currentPath, |
| 142 | + deletingPath, |
| 143 | + expireOptions, |
| 144 | + expireStyle, |
| 145 | + expireValue, |
| 146 | + isLoading, |
| 147 | + isSharing, |
| 148 | + items, |
| 149 | + shareModalTitle, |
| 150 | + shareResult, |
| 151 | + sharingItem, |
| 152 | + showShareModal, |
| 153 | + truncated, |
| 154 | + closeShareModal, |
| 155 | + confirmShare, |
| 156 | + copyCode, |
| 157 | + copyLink, |
| 158 | + loadDirectory, |
| 159 | + openShare, |
| 160 | + removeFile |
| 161 | + } |
| 162 | +} |
0 commit comments