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'
|
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) {
|
|
|
|
return this.http({
|
2020-04-12 23:54:46 +02:00
|
|
|
url: '/tasks/' + model.taskId + '/attachments/' + model.id,
|
2019-11-24 14:16:24 +01:00
|
|
|
method: 'GET',
|
|
|
|
responseType: 'blob',
|
2020-07-14 21:26:05 +02:00
|
|
|
}).then(response => {
|
2020-09-05 22:35:52 +02:00
|
|
|
return window.URL.createObjectURL(new Blob([response.data]))
|
2020-07-14 21:26:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
download(model) {
|
|
|
|
this.getBlobUrl(model).then(url => {
|
2020-09-05 22:35:52 +02:00
|
|
|
const link = document.createElement('a')
|
|
|
|
link.href = url
|
|
|
|
link.setAttribute('download', model.file.name)
|
|
|
|
link.click()
|
|
|
|
window.URL.revokeObjectURL(url)
|
2020-07-14 21:26:05 +02:00
|
|
|
})
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|