refactor: API new
This commit is contained in:
parent
7b13566757
commit
3ae9de0538
1 changed files with 87 additions and 38 deletions
|
|
@ -139,7 +139,13 @@ const useStorage = defineStore('storageStore', () => {
|
|||
|
||||
if (arr.length >= 4) return // this system does not have more than 4 level
|
||||
|
||||
const res = await api.get<(typeof folder.value)[string]>(constructUrl(arr))
|
||||
const res = await api.post<(typeof folder.value)[string]>(
|
||||
`${import.meta.env.VITE_API_ENDPOINT}storage/list`,
|
||||
{
|
||||
operation: 'folder',
|
||||
path: arr,
|
||||
},
|
||||
)
|
||||
if (res.status === 200 && res.data && Array.isArray(res.data))
|
||||
folder.value[consistantPath(path)] = res.data.sort((a, b) =>
|
||||
a.pathname.localeCompare(b.pathname),
|
||||
|
|
@ -151,8 +157,12 @@ const useStorage = defineStore('storageStore', () => {
|
|||
|
||||
if (arr.length < 3) return // file in this system only lives in level 3 and 4
|
||||
|
||||
const res = await api.get<(typeof file.value)[string]>(
|
||||
constructUrl(arr, false) + '/file',
|
||||
const res = await api.post<(typeof file.value)[string]>(
|
||||
`${import.meta.env.VITE_API_ENDPOINT}storage/list`,
|
||||
{
|
||||
operation: 'file',
|
||||
path: arr,
|
||||
},
|
||||
)
|
||||
if (res.status === 200 && res.data && Array.isArray(res.data))
|
||||
file.value[consistantPath(path)] = res.data.sort((a, b) =>
|
||||
|
|
@ -194,7 +204,7 @@ const useStorage = defineStore('storageStore', () => {
|
|||
socket.on('connect', () => console.info('Socket.io connected.'))
|
||||
socket.on('disconnect', () => console.info('Socket.io disconnected.'))
|
||||
socket.on(
|
||||
'CreateFolder',
|
||||
'FolderCreate',
|
||||
(data: {
|
||||
pathname: string
|
||||
name: string
|
||||
|
|
@ -224,7 +234,7 @@ const useStorage = defineStore('storageStore', () => {
|
|||
// NOTE:
|
||||
// API planned to make new endpoint that can move and rename in one go.
|
||||
// Need to change if api handle move and rename file instead of just edit.
|
||||
socket.on('EditFolder', (data: { from: string; to: string }) => {
|
||||
socket.on('FolderMove', (data: { from: string; to: string }) => {
|
||||
const src = data.from.split('/').filter(Boolean)
|
||||
const dst = data.to.split('/').filter(Boolean)
|
||||
const path = consistantPath(src.slice(0, -1))
|
||||
|
|
@ -261,7 +271,7 @@ const useStorage = defineStore('storageStore', () => {
|
|||
}
|
||||
}
|
||||
})
|
||||
socket.on('DeleteFolder', (data: { pathname: string }) => {
|
||||
socket.on('FolderDelete', (data: { pathname: string }) => {
|
||||
for (let key in folder.value) {
|
||||
if (key.startsWith(data.pathname)) {
|
||||
delete folder.value[key]
|
||||
|
|
@ -317,20 +327,17 @@ const useStorage = defineStore('storageStore', () => {
|
|||
)
|
||||
}
|
||||
})
|
||||
socket.on(
|
||||
'FileUpdateMove',
|
||||
(data: { from: StorageFile; to: StorageFile }) => {
|
||||
const arr = data.from.pathname.split('/').filter(Boolean)
|
||||
const path = consistantPath(arr.slice(0, -1))
|
||||
socket.on('FileMove', (data: { from: StorageFile; to: StorageFile }) => {
|
||||
const arr = data.from.pathname.split('/').filter(Boolean)
|
||||
const path = consistantPath(arr.slice(0, -1))
|
||||
|
||||
if (file.value[path]) {
|
||||
const idx = file.value[path].findIndex(
|
||||
(v) => v.pathname === data.from.pathname,
|
||||
)
|
||||
if (idx !== -1) file.value[path][idx] = data.to
|
||||
}
|
||||
},
|
||||
)
|
||||
if (file.value[path]) {
|
||||
const idx = file.value[path].findIndex(
|
||||
(v) => v.pathname === data.from.pathname,
|
||||
)
|
||||
if (idx !== -1) file.value[path][idx] = data.to
|
||||
}
|
||||
})
|
||||
socket.on('FileUpdate', (data: StorageFile) => {
|
||||
const arr = data.pathname.split('/').filter(Boolean)
|
||||
const path = consistantPath(arr.slice(0, -1))
|
||||
|
|
@ -369,42 +376,67 @@ const useStorage = defineStore('storageStore', () => {
|
|||
msg: `พบชื่อ \"${name}\" ซ้ำในระบบ`,
|
||||
})
|
||||
} else {
|
||||
await api.post(constructUrl(path, true), { name })
|
||||
const arrayPath: string[] = path.split('/').filter(Boolean)
|
||||
await api.post(`${import.meta.env.VITE_API_ENDPOINT}storage/folder`, {
|
||||
path: arrayPath,
|
||||
name: name,
|
||||
})
|
||||
}
|
||||
loader.hide()
|
||||
}
|
||||
async function editFolder(name: string, path: string) {
|
||||
loader.show()
|
||||
await api.put(constructUrl(path, false), { name })
|
||||
const arrayPath: string[] = path.split('/').filter(Boolean)
|
||||
const beforeName = arrayPath.pop()
|
||||
|
||||
await api.put(`${import.meta.env.VITE_API_ENDPOINT}storage/folder`, {
|
||||
from: {
|
||||
name: beforeName,
|
||||
path: arrayPath,
|
||||
},
|
||||
to: {
|
||||
name: name,
|
||||
path: arrayPath,
|
||||
},
|
||||
})
|
||||
loader.hide()
|
||||
}
|
||||
async function deleteFolder(path: string) {
|
||||
loader.show()
|
||||
await api.delete(constructUrl(path, false))
|
||||
await api.delete<(typeof file.value)[string]>(
|
||||
`${import.meta.env.VITE_API_ENDPOINT}storage/folder`,
|
||||
{ data: { path: path.split('/').filter(Boolean) } },
|
||||
)
|
||||
loader.hide()
|
||||
}
|
||||
|
||||
type FileMetadata = {
|
||||
title?: string
|
||||
description?: string
|
||||
keyword?: string[]
|
||||
category?: string[]
|
||||
keyword?: string[]
|
||||
}
|
||||
async function createFile(
|
||||
file: File,
|
||||
data: FileMetadata,
|
||||
path: string = currentInfo.path,
|
||||
) {
|
||||
if (path.split('/').filter(Boolean).length < 3) return // the system only allow file to live in level 3 and 4
|
||||
const arr = path.split('/').filter(Boolean)
|
||||
|
||||
if (arr.length < 3) return // the system only allow file to live in level 3 and 4
|
||||
|
||||
loader.show()
|
||||
const res = await api.post(constructUrl(path, false) + '/file', {
|
||||
file: file.name,
|
||||
...data,
|
||||
})
|
||||
if (res && res.status === 201 && res.data && res.data.upload) {
|
||||
const res = await api.post(
|
||||
`${import.meta.env.VITE_API_ENDPOINT}storage/file`,
|
||||
{
|
||||
path: arr,
|
||||
file: file.name,
|
||||
...data,
|
||||
},
|
||||
)
|
||||
if (res && res.status === 200 && res.data && res.data.uploadUrl) {
|
||||
await axios
|
||||
.put(res.data.upload, file, {
|
||||
.put(res.data.uploadUrl, file, {
|
||||
headers: { 'Content-Type': file.type },
|
||||
onUploadProgress: (e) => console.log(e),
|
||||
})
|
||||
|
|
@ -418,13 +450,29 @@ const useStorage = defineStore('storageStore', () => {
|
|||
if (arr.length < 4) return // the system only allow file to live in level 3 and 4
|
||||
|
||||
loader.show()
|
||||
const res = await api.patch(
|
||||
constructUrl(arr.slice(0, -1), false) + `/file/${arr[arr.length - 1]}`,
|
||||
{ file: file?.name, ...data },
|
||||
|
||||
const srcFile = arr.pop()
|
||||
|
||||
const res = await api.put(
|
||||
`${import.meta.env.VITE_API_ENDPOINT}storage/file`,
|
||||
{
|
||||
...data,
|
||||
from: {
|
||||
file: srcFile,
|
||||
path: arr,
|
||||
},
|
||||
to: file?.name
|
||||
? {
|
||||
file: file.name,
|
||||
path: arr,
|
||||
}
|
||||
: undefined,
|
||||
upload: !!file,
|
||||
},
|
||||
)
|
||||
if (res && res.status === 200 && res.data && res.data.upload) {
|
||||
if (res && res.status === 204 && res.data && res.data.uploadUrl) {
|
||||
await axios
|
||||
.put(res.data.upload, file, {
|
||||
.put(res.data.uploadUrl, file, {
|
||||
headers: { 'Content-Type': file?.type },
|
||||
onUploadProgress: (e) => console.log(e),
|
||||
})
|
||||
|
|
@ -438,9 +486,10 @@ const useStorage = defineStore('storageStore', () => {
|
|||
if (arr.length < 4) return // the system only allow file to live in level 3 and 4
|
||||
|
||||
loader.show()
|
||||
await api.delete(
|
||||
constructUrl(arr.slice(0, -1), false) + `/file/${arr[arr.length - 1]}`,
|
||||
)
|
||||
|
||||
await api.delete(`${import.meta.env.VITE_API_ENDPOINT}storage/file`, {
|
||||
data: { path: arr.slice(0, -1), file: arr[arr.length - 1] },
|
||||
})
|
||||
loader.hide()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue