Remove previous avatar on uploading new one (#822)

Signed-off-by: Ilya Sumbatyants <ilya.sumb@gmail.com>
This commit is contained in:
Ilya Sumbatyants 2022-01-14 16:09:01 +07:00 committed by GitHub
parent c568bbf711
commit cd6148436f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 3 deletions

View File

@ -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
}
})

View File

@ -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')
}
}

View 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>>
}
})

View File

@ -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

View File

@ -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) {