Feature: add error handling for webdav image preview component

This commit is contained in:
萌萌哒赫萝
2023-08-13 07:41:02 -07:00
parent f6c9a789d1
commit c36a5b85cc
6 changed files with 80 additions and 137 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

After

Width:  |  Height:  |  Size: 50 KiB

+29 -29
View File
@@ -1,8 +1,6 @@
<template> <template>
<el-image <el-image
:src="isShowThumbnail && item.isImage ? :src="imageSource"
base64Url
: require(`../manage/pages/assets/icons/${getFileIconPath(item.fileName ?? '')}`)"
fit="contain" fit="contain"
style="height: 100px;width: 100%;margin: 0 auto;" style="height: 100px;width: 100%;margin: 0 auto;"
> >
@@ -13,7 +11,7 @@
</template> </template>
<template #error> <template #error>
<el-image <el-image
:src="require(`../manage/pages/assets/icons/${getFileIconPath(item.fileName ?? '')}`)" :src="iconPath"
fit="contain" fit="contain"
style="height: 100px;width: 100%;margin: 0 auto;" style="height: 100px;width: 100%;margin: 0 auto;"
/> />
@@ -22,11 +20,13 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { ref, onMounted, watch } from 'vue' import { ref, onMounted, watch, computed } from 'vue'
import { getFileIconPath } from '@/manage/utils/common' import { getFileIconPath } from '@/manage/utils/common'
import { Loading } from '@element-plus/icons-vue' import { Loading } from '@element-plus/icons-vue'
const base64Url = ref('') const base64Url = ref('')
const success = ref(false)
const props = defineProps( const props = defineProps(
{ {
isShowThumbnail: { isShowThumbnail: {
@@ -48,32 +48,32 @@ const props = defineProps(
} }
) )
watch( const imageSource = computed(() => {
() => [props.url, props.headers], return (props.isShowThumbnail && props.item.isImage && success.value)
async (newValues, oldValues) => { ? base64Url.value
if (newValues[0] !== oldValues[0] || newValues[1] !== oldValues[1]) { : require(`../manage/pages/assets/icons/${getFileIconPath(props.item.fileName ?? '')}`)
try { })
await urlCreateObjectURL()
} catch (e) {
console.log(e)
}
}
},
{ deep: true }
)
const urlCreateObjectURL = async () => { const iconPath = computed(() => require(`../manage/pages/assets/icons/${getFileIconPath(props.item.fileName ?? '')}`))
await fetch(props.url, {
method: 'GET', const fetchImage = async () => {
headers: props.headers try {
}).then(res => res.blob()).then(blob => { const res = await fetch(props.url, { method: 'GET', headers: props.headers })
base64Url.value = URL.createObjectURL(blob) if (res.status >= 200 && res.status < 300) {
}).catch(err => { const blob = await res.blob()
success.value = true
base64Url.value = URL.createObjectURL(blob)
} else {
throw new Error('Network response was not ok.')
}
} catch (err) {
success.value = false
console.log(err) console.log(err)
}) }
} }
onMounted(async () => { watch(() => [props.url, props.headers], fetchImage, { deep: true })
await urlCreateObjectURL()
}) onMounted(fetchImage)
</script> </script>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

+51 -108
View File
@@ -2293,35 +2293,20 @@ watch(searchText, () => searchAndSort())
function searchAndSort () { function searchAndSort () {
fileTable.value.scrollToRow(0) fileTable.value.scrollToRow(0)
const shouldIgnoreCase = isIgnoreCase.value
currentPageFilesInfo.forEach((item: any) => {
const fileName = shouldIgnoreCase ? item.fileName.toLowerCase() : item.fileName
const search = shouldIgnoreCase ? searchText.value.toLowerCase() : searchText.value
item.match = searchText.value ? fileName.includes(search) : true
})
if (searchText.value) { if (searchText.value) {
if (isIgnoreCase.value) { currentPageFilesInfo.sort((a: any, b: any) => b.match - a.match)
currentPageFilesInfo.forEach((item: any) => {
if (item.fileName.toLowerCase().includes(searchText.value.toLowerCase())) {
item.match = true
} else {
item.match = false
}
})
currentPageFilesInfo.sort((a: any, b: any) => {
return b.match - a.match
})
} else {
currentPageFilesInfo.forEach((item: any) => {
if (item.fileName.includes(searchText.value)) {
item.match = true
} else {
item.match = false
}
})
currentPageFilesInfo.sort((a: any, b: any) => {
return b.match - a.match
})
}
} else { } else {
currentPageFilesInfo.forEach((item: any) => { const sortType = localStorage.getItem('sortType') as 'name' | 'size' | 'time' | 'ext' | 'check' | 'init' || 'init'
item.match = true sortFile(sortType)
})
sortFile(localStorage.getItem('sortType') as 'name' | 'size' | 'time' | 'ext' | 'check' | 'init' || 'init')
} }
} }
@@ -2407,20 +2392,18 @@ function handleCheckChangeOther (item: any) {
} }
function handleCheckChange (item: any) { function handleCheckChange (item: any) {
const index = currentPageFilesInfo.findIndex((i: any) => i.fileName === item.fileName)
if (item.checked) { if (item.checked) {
if (lastChoosed.value !== -1 && isShiftKeyPress.value) { if (lastChoosed.value !== -1 && isShiftKeyPress.value) {
const index = currentPageFilesInfo.findIndex((i: any) => i.fileName === item.fileName) const [start, end] = [lastChoosed.value, index].sort((a, b) => a - b)
const start = Math.min(lastChoosed.value, index)
const end = Math.max(lastChoosed.value, index)
for (let i = start + 1; i <= end; i++) { for (let i = start + 1; i <= end; i++) {
currentPageFilesInfo[i].checked = true currentPageFilesInfo[i].checked = true
selectedItems.push(currentPageFilesInfo[i]) selectedItems.push(currentPageFilesInfo[i])
} }
lastChoosed.value = index
} else { } else {
lastChoosed.value = currentPageFilesInfo.findIndex((i: any) => i.fileName === item.fileName)
selectedItems.push(item) selectedItems.push(item)
} }
lastChoosed.value = index
} else { } else {
selectedItems.splice(selectedItems.findIndex((i: any) => i.fileName === item.fileName), 1) selectedItems.splice(selectedItems.findIndex((i: any) => i.fileName === item.fileName), 1)
} }
@@ -2538,35 +2521,14 @@ async function handleBatchDownload () {
} }
function handleCheckAllChange () { function handleCheckAllChange () {
if (searchText.value === '') { const isSearchEmpty = searchText.value === ''
if (selectedItems.length === currentPageFilesInfo.length) { const itemsToCheck = isSearchEmpty ? currentPageFilesInfo : currentPageFilesInfo.filter((item: any) => item.match)
selectedItems.length = 0 const allSelected = selectedItems.length === itemsToCheck.length
currentPageFilesInfo.forEach((item: any) => { selectedItems.length = 0
item.checked = false currentPageFilesInfo.forEach((item: any) => {
}) item.checked = !allSelected && (isSearchEmpty || item.match)
} else { if (item.checked) selectedItems.push(item)
selectedItems.length = 0 })
selectedItems.push(...currentPageFilesInfo)
currentPageFilesInfo.forEach((item: any) => {
item.checked = true
})
}
} else {
if (selectedItems.length === currentPageFilesInfo.filter((item: any) => item.match).length) {
selectedItems.length = 0
currentPageFilesInfo.forEach((item: any) => {
item.checked = false
})
} else {
selectedItems.length = 0
currentPageFilesInfo.forEach((item: any) => {
if (item.match) {
item.checked = true
selectedItems.push(item)
}
})
}
}
} }
function handleCreateFolder () { function handleCreateFolder () {
@@ -2901,47 +2863,37 @@ async function getBucketFileList () {
} }
function handleBatchDeleteInfo () { function handleBatchDeleteInfo () {
ElMessageBox.confirm(`${$T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_TITLE_A')} ${selectedItems.length} ${$T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_TITLE_B')}`, $T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_MSG'), { const confirmTitle = `${$T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_TITLE_A')} ${selectedItems.length} ${$T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_TITLE_B')}`
ElMessageBox.confirm(confirmTitle, $T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_MSG'), {
confirmButtonText: $T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_CONFIRM'), confirmButtonText: $T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_CONFIRM'),
cancelButtonText: $T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_CANCEL'), cancelButtonText: $T('MANAGE_BUCKET_BATCH_DELETE_CONFIRM_CANCEL'),
type: 'warning', type: 'warning',
center: true, center: true,
draggable: true draggable: true
}).then(async () => { }).then(async () => {
const copyedSelectedItems = JSON.parse(JSON.stringify(selectedItems)) const copiedSelectedItems = JSON.parse(JSON.stringify(selectedItems))
let successCount = 0 let successCount = 0
let failCount = 0 let failCount = 0
let res = false
for (let i = 0; i < copyedSelectedItems.length; i++) { for (const item of copiedSelectedItems) {
if (!copyedSelectedItems[i].isDir) { const param = {
const param = { bucketName: configMap.bucketName,
// tcyun region: configMap.bucketConfig.Location,
bucketName: configMap.bucketName, key: item.key,
region: configMap.bucketConfig.Location, DeleteHash: item.sha,
key: copyedSelectedItems[i].key, githubBranch: currentCustomDomain.value
DeleteHash: copyedSelectedItems[i].sha,
githubBranch: currentCustomDomain.value
}
res = await ipcRenderer.invoke('deleteBucketFile', configMap.alias, param)
} else {
const param = {
// tcyun
bucketName: configMap.bucketName,
region: configMap.bucketConfig.Location,
key: copyedSelectedItems[i].key,
githubBranch: currentCustomDomain.value,
DeleteHash: copyedSelectedItems[i].sha
}
res = await ipcRenderer.invoke('deleteBucketFolder', configMap.alias, param)
} }
if (res) { const result = item.isDir
? await ipcRenderer.invoke('deleteBucketFolder', configMap.alias, param)
: await ipcRenderer.invoke('deleteBucketFile', configMap.alias, param)
if (result) {
successCount++ successCount++
currentPageFilesInfo.splice(currentPageFilesInfo.findIndex((j: any) => j.key === copyedSelectedItems[i].key), 1) currentPageFilesInfo.splice(currentPageFilesInfo.findIndex((j: any) => j.key === item.key), 1)
selectedItems.splice(selectedItems.findIndex((j: any) => j.key === copyedSelectedItems[i].key), 1) selectedItems.splice(selectedItems.findIndex((j: any) => j.key === item.key), 1)
if (!paging.value) { if (!paging.value) {
const table = fileCacheDbInstance.table(currentPicBedName.value) const table = fileCacheDbInstance.table(currentPicBedName.value)
table.where('key').equals(getTableKeyOfDb()).modify((l: any) => { table.where('key').equals(getTableKeyOfDb()).modify((l: any) => {
l.value.fullList.splice(l.value.fullList.findIndex((j: any) => j.key === copyedSelectedItems[i].key), 1) l.value.fullList.splice(l.value.fullList.findIndex((j: any) => j.key === item.key), 1)
}) })
} }
} else { } else {
@@ -2981,31 +2933,22 @@ function handleDeleteFile (item: any) {
draggable: true draggable: true
}).then(async () => { }).then(async () => {
let res = false let res = false
if (!item.isDir) { const param = {
const param = { bucketName: configMap.bucketName,
// tcyun region: configMap.bucketConfig.Location,
bucketName: configMap.bucketName, key: item.key,
region: configMap.bucketConfig.Location, DeleteHash: item.sha,
key: item.key, githubBranch: currentCustomDomain.value
DeleteHash: item.sha, }
githubBranch: currentCustomDomain.value if (item.isDir) {
}
res = await ipcRenderer.invoke('deleteBucketFile', configMap.alias, param)
} else {
const param = {
// tcyun
bucketName: configMap.bucketName,
region: configMap.bucketConfig.Location,
key: item.key,
DeleteHash: item.sha,
githubBranch: currentCustomDomain.value
}
ElNotification.info({ ElNotification.info({
title: $T('MANAGE_BUCKET_DELETE_ERROR_MSG_TITLE'), title: $T('MANAGE_BUCKET_DELETE_ERROR_MSG_TITLE'),
message: $T('MANAGE_BUCKET_DELETE_ERROR_MSG_MSG'), message: $T('MANAGE_BUCKET_DELETE_ERROR_MSG_MSG'),
duration: 1000 duration: 1000
}) })
res = await ipcRenderer.invoke('deleteBucketFolder', configMap.alias, param) res = await ipcRenderer.invoke('deleteBucketFolder', configMap.alias, param)
} else {
res = await ipcRenderer.invoke('deleteBucketFile', configMap.alias, param)
} }
if (res) { if (res) {
ElMessage.success($T('MANAGE_BUCKET_DELETE_SUCCESS')) ElMessage.success($T('MANAGE_BUCKET_DELETE_SUCCESS'))