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({
|
|
|
|
create: '/tasks/{task_id}/attachments',
|
|
|
|
getAll: '/tasks/{task_id}/attachments',
|
|
|
|
delete: '/tasks/{task_id}/attachments/{id}',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-08 14:16:06 +01:00
|
|
|
processModel(model) {
|
2020-03-02 21:55:22 +01:00
|
|
|
model.created = formatISO(model.created)
|
2020-02-08 14:16:06 +01:00
|
|
|
return model
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:16:24 +01:00
|
|
|
uploadProgress = 0
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
download(model) {
|
|
|
|
this.http({
|
|
|
|
url: '/tasks/' + model.task_id + '/attachments/' + model.id,
|
|
|
|
method: 'GET',
|
|
|
|
responseType: 'blob',
|
|
|
|
}).then((response) => {
|
|
|
|
const url = window.URL.createObjectURL(new Blob([response.data]));
|
|
|
|
const link = document.createElement('a');
|
|
|
|
link.href = url;
|
|
|
|
link.setAttribute('download', model.file.name);
|
|
|
|
link.click();
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads a file to the server
|
|
|
|
* @param model
|
|
|
|
* @param files
|
|
|
|
* @returns {Promise<any|never>}
|
|
|
|
*/
|
|
|
|
create(model, files) {
|
|
|
|
|
|
|
|
let data = new FormData()
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
// TODO: Validation of file size
|
|
|
|
data.append('files', new Blob([files[i]]), files[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cancel = this.setLoading()
|
|
|
|
return this.http.put(
|
|
|
|
this.getReplacedRoute(this.paths.create, model),
|
|
|
|
data,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
'Content-Type':
|
|
|
|
'multipart/form-data; boundary=' + data._boundary,
|
|
|
|
},
|
|
|
|
onUploadProgress: progressEvent => {
|
|
|
|
this.uploadProgress = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.catch(error => {
|
|
|
|
return this.errorHandler(error)
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
return Promise.resolve(this.modelCreateFactory(response.data))
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.uploadProgress = 0
|
|
|
|
cancel()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|