mirror of
https://github.com/hcengineering/platform.git
synced 2024-12-23 03:22:19 +03:00
Remove previous avatar on uploading new one (#822)
Signed-off-by: Ilya Sumbatyants <ilya.sumb@gmail.com>
This commit is contained in:
parent
c568bbf711
commit
cd6148436f
@ -17,7 +17,7 @@ import AttachmentsPresenter from './components/AttachmentsPresenter.svelte'
|
||||
import AttachmentPresenter from './components/AttachmentPresenter.svelte'
|
||||
import TxAttachmentCreate from './components/activity/TxAttachmentCreate.svelte'
|
||||
import Attachments from './components/Attachments.svelte'
|
||||
import { uploadFile } from './utils'
|
||||
import { uploadFile, deleteFile } from './utils'
|
||||
|
||||
export { Attachments, AttachmentsPresenter }
|
||||
|
||||
@ -31,6 +31,7 @@ export default async () => ({
|
||||
TxAttachmentCreate
|
||||
},
|
||||
helper: {
|
||||
UploadFile: uploadFile
|
||||
UploadFile: uploadFile,
|
||||
DeleteFile: deleteFile
|
||||
}
|
||||
})
|
||||
|
@ -45,3 +45,19 @@ export async function uploadFile (file: File, space?: Ref<Space>, attachedTo?: R
|
||||
console.log(uuid)
|
||||
return uuid
|
||||
}
|
||||
|
||||
export async function deleteFile (id: string): Promise<void> {
|
||||
const uploadUrl = getMetadata(login.metadata.UploadUrl)
|
||||
|
||||
const url = `${uploadUrl as string}?file=${id}`
|
||||
const resp = await fetch(url, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: 'Bearer ' + (getMetadata(login.metadata.LoginToken) as string)
|
||||
}
|
||||
})
|
||||
|
||||
if (resp.status !== 200) {
|
||||
throw new Error('Failed to delete file')
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ export default plugin(attachmentId, {
|
||||
Attachment: '' as Ref<Class<Attachment>>
|
||||
},
|
||||
helper: {
|
||||
UploadFile: '' as Resource<(file: File, space?: Ref<Space>, attachedTo?: Ref<Doc>) => Promise<string>>
|
||||
UploadFile: '' as Resource<(file: File, space?: Ref<Space>, attachedTo?: Ref<Doc>) => Promise<string>>,
|
||||
DeleteFile: '' as Resource<(id: string) => Promise<void>>
|
||||
}
|
||||
})
|
||||
|
@ -66,8 +66,12 @@
|
||||
|
||||
async function onAvatarDone (e: any) {
|
||||
const uploadFile = await getResource(attachment.helper.UploadFile)
|
||||
const deleteFile = await getResource(attachment.helper.DeleteFile)
|
||||
const { file: avatar } = e.detail
|
||||
|
||||
if (object.avatar != null) {
|
||||
await deleteFile(object.avatar)
|
||||
}
|
||||
const uuid = await uploadFile(avatar)
|
||||
await client.updateDoc(object._class, object.space, object._id, {
|
||||
avatar: uuid
|
||||
|
@ -198,6 +198,28 @@ export function start (config: { transactorEndpoint: string, elasticUrl: string,
|
||||
}
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
app.delete('/files', async (req, res) => {
|
||||
try {
|
||||
const authHeader = req.headers.authorization
|
||||
if (authHeader === undefined) {
|
||||
res.status(403).send()
|
||||
return
|
||||
}
|
||||
|
||||
const token = authHeader.split(' ')[1]
|
||||
const payload = decode(token ?? '', 'secret', false) as Token
|
||||
const uuid = req.query.file as string
|
||||
|
||||
await config.minio.removeObject(payload.workspace, uuid)
|
||||
|
||||
res.status(200).send()
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
res.status(500).send()
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/import', (req, res) => {
|
||||
const authHeader = req.headers.authorization
|
||||
if (authHeader === undefined) {
|
||||
|
Loading…
Reference in New Issue
Block a user