2019-11-24 14:16:24 +01:00
|
|
|
import AbstractService from './abstractService'
|
2022-07-21 00:42:36 +02:00
|
|
|
import AttachmentModel, { type IAttachment } from '../models/attachment'
|
2020-03-02 21:55:22 +01:00
|
|
|
import {formatISO} from 'date-fns'
|
2021-09-04 21:26:38 +02:00
|
|
|
import {downloadBlob} from '@/helpers/downloadBlob'
|
2022-07-21 00:42:36 +02:00
|
|
|
import type { IFile } from '@/models/file'
|
2019-11-24 14:16:24 +01:00
|
|
|
|
2022-07-20 21:15:35 +02:00
|
|
|
export default class AttachmentService extends AbstractService<AttachmentModel> {
|
2019-11-24 14:16:24 +01:00
|
|
|
constructor() {
|
|
|
|
super({
|
2020-04-12 23:54:46 +02:00
|
|
|
create: '/tasks/{taskId}/attachments',
|
|
|
|
getAll: '/tasks/{taskId}/attachments',
|
|
|
|
delete: '/tasks/{taskId}/attachments/{id}',
|
2019-11-24 14:16:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:42:36 +02:00
|
|
|
processModel(model: IAttachment) {
|
2020-09-04 22:01:02 +02:00
|
|
|
model.created = formatISO(new Date(model.created))
|
2020-02-08 14:16:06 +01:00
|
|
|
return model
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:16:24 +01:00
|
|
|
useCreateInterceptor() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
modelFactory(data) {
|
|
|
|
return new AttachmentModel(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
modelCreateFactory(data) {
|
|
|
|
// Success contains the uploaded attachments
|
|
|
|
data.success = (data.success === null ? [] : data.success).map(a => {
|
|
|
|
return this.modelFactory(a)
|
|
|
|
})
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:42:36 +02:00
|
|
|
getBlobUrl(model: IAttachment) {
|
2021-09-04 21:26:38 +02:00
|
|
|
return AbstractService.prototype.getBlobUrl.call(this, '/tasks/' + model.taskId + '/attachments/' + model.id)
|
2020-07-14 21:26:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 00:42:36 +02:00
|
|
|
async download(model: IAttachment) {
|
2021-10-09 16:34:57 +02:00
|
|
|
const url = await this.getBlobUrl(model)
|
|
|
|
return downloadBlob(url, model.file.name)
|
2019-11-24 14:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads a file to the server
|
|
|
|
* @param files
|
|
|
|
* @returns {Promise<any|never>}
|
|
|
|
*/
|
2022-07-21 00:42:36 +02:00
|
|
|
create(model: IAttachment, files: IFile[]) {
|
2020-08-02 19:17:29 +02:00
|
|
|
const data = new FormData()
|
2019-11-24 14:16:24 +01:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
// TODO: Validation of file size
|
2022-07-20 21:15:35 +02:00
|
|
|
data.append('files', new Blob([JSON.stringify(files[i], null, 2)]), files[i].name)
|
2019-11-24 14:16:24 +01:00
|
|
|
}
|
|
|
|
|
2020-08-02 19:17:29 +02:00
|
|
|
return this.uploadFormData(
|
2019-11-24 14:16:24 +01:00
|
|
|
this.getReplacedRoute(this.paths.create, model),
|
2020-09-05 22:35:52 +02:00
|
|
|
data,
|
2019-11-24 14:16:24 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|