mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
Refactor FileDisk system with per-disk unique names and disk assignments UI
Major changes to the file disk subsystem:
- Each FileDisk now gets a unique Laravel disk name (disk_{id}) instead
of temp_{driver}, fixing the bug where multiple local disks with
different roots overwrote each other's config.
- Move disk registration logic from FileDisk model to FileDiskService
(registerDisk, getDiskName). Model keeps only getDecodedCredentials
and a deprecated setConfig() wrapper.
- Add Disk Assignments admin UI (File Disk tab) with three purpose
dropdowns: Media Storage, PDF Storage, Backup Storage. Stored as
settings (media_disk_id, pdf_disk_id, backup_disk_id).
- Backup tab now uses the assigned backup disk instead of a per-backup
dropdown. BackupsController refactored to use BackupService which
centralizes disk resolution. Removed stale 4-second cache.
- Add local_public disk to config/filesystems.php so system disks
are properly defined.
- Local disk roots stored relative to storage/app/ with hint text
in the admin modal explaining the convention.
- Fix BaseModal watchEffect -> watch to prevent infinite request
loops on the File Disk page.
- Fix string/number comparison for disk purpose IDs from settings.
- Add safeguards: prevent deleting disks with files, warn on
purpose change, prevent deleting system disks.
This commit is contained in:
@@ -9,7 +9,6 @@ import {
|
||||
backupService,
|
||||
type CreateBackupPayload,
|
||||
} from '@v2/api/services/backup.service'
|
||||
import { diskService, type Disk } from '@v2/api/services/disk.service'
|
||||
import {
|
||||
getErrorTranslationKey,
|
||||
handleApiError,
|
||||
@@ -17,23 +16,14 @@ import {
|
||||
|
||||
type BackupOption = CreateBackupPayload['option']
|
||||
|
||||
interface DiskOption extends Disk {
|
||||
display_name: string
|
||||
}
|
||||
|
||||
interface BackupTypeOption {
|
||||
id: BackupOption
|
||||
label: string
|
||||
}
|
||||
|
||||
interface BackupModalData {
|
||||
disks?: DiskOption[]
|
||||
selectedDiskId?: number | null
|
||||
}
|
||||
|
||||
interface BackupForm {
|
||||
option: BackupOption | ''
|
||||
selectedDiskId: number | null
|
||||
file_disk_id: number | null
|
||||
}
|
||||
|
||||
const modalStore = useModalStore()
|
||||
@@ -41,27 +31,16 @@ const notificationStore = useNotificationStore()
|
||||
const { t } = useI18n()
|
||||
|
||||
const isSaving = ref(false)
|
||||
const isFetchingInitialData = ref(false)
|
||||
const disks = ref<DiskOption[]>([])
|
||||
|
||||
const form = reactive<BackupForm>({
|
||||
option: 'full',
|
||||
selectedDiskId: null,
|
||||
file_disk_id: null,
|
||||
})
|
||||
|
||||
const backupTypeOptions: BackupTypeOption[] = [
|
||||
{
|
||||
id: 'full',
|
||||
label: 'full',
|
||||
},
|
||||
{
|
||||
id: 'only-db',
|
||||
label: 'only-db',
|
||||
},
|
||||
{
|
||||
id: 'only-files',
|
||||
label: 'only-files',
|
||||
},
|
||||
{ id: 'full', label: 'full' },
|
||||
{ id: 'only-db', label: 'only-db' },
|
||||
{ id: 'only-files', label: 'only-files' },
|
||||
]
|
||||
|
||||
const modalActive = computed<boolean>(() => {
|
||||
@@ -72,61 +51,23 @@ const rules = computed(() => ({
|
||||
option: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
selectedDiskId: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
}))
|
||||
|
||||
const v$ = useVuelidate(rules, form)
|
||||
|
||||
async function setInitialData(): Promise<void> {
|
||||
function setInitialData(): void {
|
||||
resetForm()
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
try {
|
||||
const modalData = isBackupModalData(modalStore.data) ? modalStore.data : null
|
||||
|
||||
if (modalData?.disks?.length) {
|
||||
disks.value = modalData.disks
|
||||
form.selectedDiskId =
|
||||
modalData.selectedDiskId ??
|
||||
modalData.disks.find((disk) => disk.set_as_default)?.id ??
|
||||
modalData.disks[0]?.id ??
|
||||
null
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const response = await diskService.list({ limit: 'all' })
|
||||
|
||||
disks.value = response.data.map((disk) => ({
|
||||
...disk,
|
||||
display_name: `${disk.name} - [${disk.driver}]`,
|
||||
}))
|
||||
|
||||
const selectedDiskId =
|
||||
modalStore.data &&
|
||||
typeof modalStore.data === 'object' &&
|
||||
'id' in (modalStore.data as Record<string, unknown>)
|
||||
? Number((modalStore.data as Record<string, unknown>).id)
|
||||
: null
|
||||
|
||||
form.selectedDiskId =
|
||||
disks.value.find((disk) => disk.id === selectedDiskId)?.id ??
|
||||
disks.value.find((disk) => disk.set_as_default)?.id ??
|
||||
disks.value[0]?.id ??
|
||||
null
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isFetchingInitialData.value = false
|
||||
const modalData = modalStore.data as Record<string, unknown> | null
|
||||
if (modalData?.file_disk_id) {
|
||||
form.file_disk_id = Number(modalData.file_disk_id)
|
||||
}
|
||||
}
|
||||
|
||||
async function createBackup(): Promise<void> {
|
||||
v$.value.$touch()
|
||||
|
||||
if (v$.value.$invalid || !form.selectedDiskId) {
|
||||
if (v$.value.$invalid || !form.file_disk_id) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -135,7 +76,7 @@ async function createBackup(): Promise<void> {
|
||||
try {
|
||||
const response = await backupService.create({
|
||||
option: form.option as BackupOption,
|
||||
file_disk_id: form.selectedDiskId,
|
||||
file_disk_id: form.file_disk_id,
|
||||
})
|
||||
|
||||
if (response.success) {
|
||||
@@ -165,7 +106,7 @@ function showApiError(error: unknown): void {
|
||||
|
||||
function resetForm(): void {
|
||||
form.option = 'full'
|
||||
form.selectedDiskId = null
|
||||
form.file_disk_id = null
|
||||
v$.value.$reset()
|
||||
}
|
||||
|
||||
@@ -174,13 +115,8 @@ function closeModal(): void {
|
||||
|
||||
setTimeout(() => {
|
||||
resetForm()
|
||||
disks.value = []
|
||||
}, 300)
|
||||
}
|
||||
|
||||
function isBackupModalData(value: unknown): value is BackupModalData {
|
||||
return Boolean(value && typeof value === 'object')
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -212,28 +148,6 @@ function isBackupModalData(value: unknown): value is BackupModalData {
|
||||
@update:model-value="v$.option.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.disk.select_disk')"
|
||||
:error="
|
||||
v$.selectedDiskId.$error && v$.selectedDiskId.$errors[0]?.$message
|
||||
"
|
||||
required
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="form.selectedDiskId"
|
||||
:options="disks"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:can-deselect="false"
|
||||
:invalid="v$.selectedDiskId.$error"
|
||||
label="display_name"
|
||||
track-by="id"
|
||||
value-prop="id"
|
||||
searchable
|
||||
:placeholder="$t('settings.disk.select_disk')"
|
||||
@update:model-value="v$.selectedDiskId.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ interface DiskField {
|
||||
key: string
|
||||
labelKey: string
|
||||
placeholder?: string
|
||||
hint?: string
|
||||
}
|
||||
|
||||
interface DiskDriverOption {
|
||||
@@ -39,7 +40,8 @@ const DRIVER_FIELDS: Record<DiskDriverValue, DiskField[]> = {
|
||||
{
|
||||
key: 'root',
|
||||
labelKey: 'settings.disk.local_root',
|
||||
placeholder: 'Ex. /user/root/',
|
||||
placeholder: 'Ex. backups',
|
||||
hint: 'settings.disk.local_root_hint',
|
||||
},
|
||||
],
|
||||
s3: [
|
||||
@@ -512,6 +514,9 @@ function isDisk(value: unknown): value is Disk {
|
||||
:placeholder="field.placeholder"
|
||||
@input="touchCredential(field.key)"
|
||||
/>
|
||||
<span v-if="field.hint" class="text-xs text-subtle mt-1 block">
|
||||
{{ $t(field.hint) }}
|
||||
</span>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { computed, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useModalStore } from '@v2/stores/modal.store'
|
||||
import { useDialogStore } from '@v2/stores/dialog.store'
|
||||
@@ -20,10 +20,6 @@ interface TableColumn {
|
||||
sortable?: boolean
|
||||
}
|
||||
|
||||
interface DiskOption extends Disk {
|
||||
display_name: string
|
||||
}
|
||||
|
||||
interface FetchParams {
|
||||
page: number
|
||||
filter: Record<string, unknown>
|
||||
@@ -46,8 +42,7 @@ const notificationStore = useNotificationStore()
|
||||
const { t } = useI18n()
|
||||
|
||||
const table = ref<{ refresh: () => void } | null>(null)
|
||||
const disks = ref<DiskOption[]>([])
|
||||
const selectedDisk = ref<DiskOption | null>(null)
|
||||
const backupDisk = ref<Disk | null>(null)
|
||||
const isFetchingInitialData = ref(false)
|
||||
const backupError = ref('')
|
||||
|
||||
@@ -68,6 +63,12 @@ const backupColumns = computed<TableColumn[]>(() => [
|
||||
label: t('settings.backup.size'),
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'disk_name',
|
||||
label: t('settings.disk.title', 1),
|
||||
tdClass: 'font-medium text-muted',
|
||||
sortable: false,
|
||||
},
|
||||
{
|
||||
key: 'actions',
|
||||
label: '',
|
||||
@@ -76,30 +77,27 @@ const backupColumns = computed<TableColumn[]>(() => [
|
||||
},
|
||||
])
|
||||
|
||||
watch(
|
||||
selectedDisk,
|
||||
(newDisk, oldDisk) => {
|
||||
if (newDisk?.id && oldDisk?.id && newDisk.id !== oldDisk.id) {
|
||||
refreshTable()
|
||||
}
|
||||
}
|
||||
)
|
||||
loadBackupDisk()
|
||||
|
||||
loadDisks()
|
||||
|
||||
async function loadDisks(): Promise<void> {
|
||||
async function loadBackupDisk(): Promise<void> {
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
try {
|
||||
const response = await diskService.list({ limit: 'all' })
|
||||
const [diskResponse, purposesResponse] = await Promise.all([
|
||||
diskService.list({ limit: 'all' }),
|
||||
diskService.getDiskPurposes(),
|
||||
])
|
||||
|
||||
disks.value = response.data.map((disk) => ({
|
||||
...disk,
|
||||
display_name: `${disk.name} - [${disk.driver}]`,
|
||||
}))
|
||||
const disks = diskResponse.data
|
||||
const backupDiskId = purposesResponse.backup_disk_id
|
||||
|
||||
selectedDisk.value =
|
||||
disks.value.find((disk) => disk.set_as_default) ?? disks.value[0] ?? null
|
||||
backupDisk.value =
|
||||
(backupDiskId ? disks.find((disk) => disk.id === Number(backupDiskId)) : null) ??
|
||||
disks.find((disk) => disk.set_as_default) ??
|
||||
disks[0] ??
|
||||
null
|
||||
// Refresh table now that we know which disk to query
|
||||
refreshTable()
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
@@ -108,7 +106,7 @@ async function loadDisks(): Promise<void> {
|
||||
}
|
||||
|
||||
async function fetchData({ page }: FetchParams): Promise<FetchResult> {
|
||||
if (!selectedDisk.value) {
|
||||
if (!backupDisk.value) {
|
||||
return emptyResult(page)
|
||||
}
|
||||
|
||||
@@ -116,8 +114,8 @@ async function fetchData({ page }: FetchParams): Promise<FetchResult> {
|
||||
|
||||
try {
|
||||
const response = await backupService.list({
|
||||
disk: selectedDisk.value.driver,
|
||||
file_disk_id: selectedDisk.value.id,
|
||||
disk: backupDisk.value.driver,
|
||||
file_disk_id: backupDisk.value.id,
|
||||
})
|
||||
|
||||
if (response.error) {
|
||||
@@ -142,7 +140,7 @@ async function fetchData({ page }: FetchParams): Promise<FetchResult> {
|
||||
}
|
||||
|
||||
async function removeBackup(backup: Backup): Promise<void> {
|
||||
if (!selectedDisk.value) {
|
||||
if (!backupDisk.value) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -162,8 +160,8 @@ async function removeBackup(backup: Backup): Promise<void> {
|
||||
|
||||
try {
|
||||
const response = await backupService.delete({
|
||||
disk: selectedDisk.value.driver,
|
||||
file_disk_id: selectedDisk.value.id,
|
||||
disk: backupDisk.value.driver,
|
||||
file_disk_id: backupDisk.value.id,
|
||||
path: backup.path,
|
||||
})
|
||||
|
||||
@@ -180,7 +178,7 @@ async function removeBackup(backup: Backup): Promise<void> {
|
||||
}
|
||||
|
||||
async function downloadBackup(backup: Backup): Promise<void> {
|
||||
if (!selectedDisk.value) {
|
||||
if (!backupDisk.value) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -189,8 +187,8 @@ async function downloadBackup(backup: Backup): Promise<void> {
|
||||
|
||||
try {
|
||||
const blob = await backupService.download({
|
||||
disk: selectedDisk.value.driver,
|
||||
file_disk_id: selectedDisk.value.id,
|
||||
disk: backupDisk.value.driver,
|
||||
file_disk_id: backupDisk.value.id,
|
||||
path: backup.path,
|
||||
})
|
||||
|
||||
@@ -216,13 +214,16 @@ async function downloadBackup(backup: Backup): Promise<void> {
|
||||
}
|
||||
|
||||
function openCreateBackupModal(): void {
|
||||
if (!backupDisk.value) {
|
||||
return
|
||||
}
|
||||
|
||||
modalStore.openModal({
|
||||
title: t('settings.backup.create_backup'),
|
||||
componentName: 'AdminBackupModal',
|
||||
size: 'sm',
|
||||
data: {
|
||||
disks: disks.value,
|
||||
selectedDiskId: selectedDisk.value?.id ?? null,
|
||||
file_disk_id: backupDisk.value.id,
|
||||
},
|
||||
refreshData: table.value?.refresh,
|
||||
})
|
||||
@@ -271,26 +272,6 @@ function showApiError(error: unknown): void {
|
||||
</BaseButton>
|
||||
</template>
|
||||
|
||||
<div class="grid my-14 md:grid-cols-3">
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.disk.select_disk')"
|
||||
:content-loading="isFetchingInitialData"
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="selectedDisk"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:options="disks"
|
||||
track-by="id"
|
||||
value-prop="id"
|
||||
label="display_name"
|
||||
:placeholder="$t('settings.disk.select_disk')"
|
||||
object
|
||||
searchable
|
||||
class="w-full"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</div>
|
||||
|
||||
<BaseErrorAlert
|
||||
v-if="backupError"
|
||||
class="mt-6"
|
||||
@@ -304,6 +285,10 @@ function showApiError(error: unknown): void {
|
||||
:data="fetchData"
|
||||
:columns="backupColumns"
|
||||
>
|
||||
<template #cell-disk_name>
|
||||
{{ backupDisk?.name ?? '-' }}
|
||||
</template>
|
||||
|
||||
<template #cell-actions="{ row }">
|
||||
<BaseDropdown>
|
||||
<template #activator>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import type { DiskPurposes } from '@v2/api/services/disk.service'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useModalStore } from '@v2/stores/modal.store'
|
||||
import { useDialogStore } from '@v2/stores/dialog.store'
|
||||
@@ -47,6 +48,79 @@ const savePdfToDisk = ref(
|
||||
(globalStore.globalSettings?.save_pdf_to_disk ?? 'NO') === 'YES'
|
||||
)
|
||||
|
||||
// Disk purpose assignments
|
||||
const allDisks = ref<Disk[]>([])
|
||||
const purposes = ref<DiskPurposes>({
|
||||
media_disk_id: null,
|
||||
pdf_disk_id: null,
|
||||
backup_disk_id: null,
|
||||
})
|
||||
const originalPurposes = ref<DiskPurposes>({
|
||||
media_disk_id: null,
|
||||
pdf_disk_id: null,
|
||||
backup_disk_id: null,
|
||||
})
|
||||
const isSavingPurposes = ref(false)
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const [disksRes, purposesRes] = await Promise.all([
|
||||
diskService.list({ limit: 'all' as unknown as number }),
|
||||
diskService.getDiskPurposes(),
|
||||
])
|
||||
allDisks.value = disksRes.data
|
||||
const normalized = {
|
||||
media_disk_id: purposesRes.media_disk_id ? Number(purposesRes.media_disk_id) : null,
|
||||
pdf_disk_id: purposesRes.pdf_disk_id ? Number(purposesRes.pdf_disk_id) : null,
|
||||
backup_disk_id: purposesRes.backup_disk_id ? Number(purposesRes.backup_disk_id) : null,
|
||||
}
|
||||
purposes.value = { ...normalized }
|
||||
originalPurposes.value = { ...normalized }
|
||||
} catch {
|
||||
// Silently fail
|
||||
}
|
||||
})
|
||||
|
||||
function hasChangedPurposes(): boolean {
|
||||
return (
|
||||
purposes.value.media_disk_id !== originalPurposes.value.media_disk_id ||
|
||||
purposes.value.pdf_disk_id !== originalPurposes.value.pdf_disk_id ||
|
||||
purposes.value.backup_disk_id !== originalPurposes.value.backup_disk_id
|
||||
)
|
||||
}
|
||||
|
||||
async function savePurposes(): Promise<void> {
|
||||
if (hasChangedPurposes()) {
|
||||
const confirmed = await dialogStore.openDialog({
|
||||
title: t('general.are_you_sure'),
|
||||
message: t('settings.disk.change_disk_warning'),
|
||||
yesLabel: t('general.ok'),
|
||||
noLabel: t('general.cancel'),
|
||||
variant: 'danger',
|
||||
hideNoButton: false,
|
||||
size: 'lg',
|
||||
})
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
isSavingPurposes.value = true
|
||||
try {
|
||||
await diskService.updateDiskPurposes(purposes.value)
|
||||
originalPurposes.value = { ...purposes.value }
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.disk.purposes_saved'),
|
||||
})
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isSavingPurposes.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const fileDiskColumns = computed<TableColumn[]>(() => [
|
||||
{
|
||||
key: 'name',
|
||||
@@ -292,4 +366,68 @@ function showApiError(error: unknown): void {
|
||||
:description="$t('settings.disk.disk_setting_description')"
|
||||
/>
|
||||
</BaseSettingCard>
|
||||
|
||||
<!-- Disk Assignments -->
|
||||
<BaseSettingCard
|
||||
:title="$t('settings.disk.disk_assignments')"
|
||||
:description="$t('settings.disk.disk_assignments_description')"
|
||||
class="mt-6"
|
||||
>
|
||||
<BaseInputGrid class="mt-4">
|
||||
<BaseInputGroup :label="$t('settings.disk.media_storage')">
|
||||
<BaseMultiselect
|
||||
v-model="purposes.media_disk_id"
|
||||
:options="allDisks"
|
||||
value-prop="id"
|
||||
label="name"
|
||||
track-by="name"
|
||||
:can-deselect="false"
|
||||
:placeholder="$t('settings.disk.select_disk')"
|
||||
/>
|
||||
<span class="text-xs text-subtle mt-1 block">
|
||||
{{ $t('settings.disk.media_storage_description') }}
|
||||
</span>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup :label="$t('settings.disk.pdf_storage')">
|
||||
<BaseMultiselect
|
||||
v-model="purposes.pdf_disk_id"
|
||||
:options="allDisks"
|
||||
value-prop="id"
|
||||
label="name"
|
||||
track-by="name"
|
||||
:can-deselect="false"
|
||||
:placeholder="$t('settings.disk.select_disk')"
|
||||
/>
|
||||
<span class="text-xs text-subtle mt-1 block">
|
||||
{{ $t('settings.disk.pdf_storage_description') }}
|
||||
</span>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup :label="$t('settings.disk.backup_storage')">
|
||||
<BaseMultiselect
|
||||
v-model="purposes.backup_disk_id"
|
||||
:options="allDisks"
|
||||
value-prop="id"
|
||||
label="name"
|
||||
track-by="name"
|
||||
:can-deselect="false"
|
||||
:placeholder="$t('settings.disk.select_disk')"
|
||||
/>
|
||||
<span class="text-xs text-subtle mt-1 block">
|
||||
{{ $t('settings.disk.backup_storage_description') }}
|
||||
</span>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
|
||||
<BaseButton
|
||||
:loading="isSavingPurposes"
|
||||
:disabled="isSavingPurposes"
|
||||
variant="primary"
|
||||
class="mt-6"
|
||||
@click="savePurposes"
|
||||
>
|
||||
{{ $t('general.save') }}
|
||||
</BaseButton>
|
||||
</BaseSettingCard>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user