2019-11-24 14:16:24 +01:00
|
|
|
import AbstractService from './abstractService'
|
|
|
|
import AttachmentModel 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'
|
2019-11-24 14:16:24 +01:00
|
|
|
|
|
|
|
export default class AttachmentService extends AbstractService {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-08 14:16:06 +01:00
|
|
|
processModel(model) {
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-14 21:26:05 +02:00
|
|
|
getBlobUrl(model) {
|
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
|
|
|
}
|
|
|
|
|
2021-10-09 16:34:57 +02:00
|
|
|
async download(model) {
|
|
|
|
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 model
|
|
|
|
* @param files
|
|
|
|
* @returns {Promise<any|never>}
|
|
|
|
*/
|
|
|
|
create(model, files) {
|
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
|
2020-09-05 22:35:52 +02:00
|
|
|
data.append('files', new Blob([files[i]]), 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
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|