More avatar providers #200

Merged
konrad merged 11 commits from feature/avatar into master 2020-08-02 17:17:36 +00:00
3 changed files with 53 additions and 53 deletions
Showing only changes of commit 05bdc48dbd - Show all commits

View File

@ -10,6 +10,7 @@ export default class AbstractService {
http = null
loading = false
uploadProgress = 0
paths = {
create: '',
get: '',
@ -443,4 +444,50 @@ export default class AbstractService {
cancel()
})
}
/**
* Uploads a file to a url.
* @param url
* @param file
* @param fieldName The name of the field the file is uploaded to.
* @returns {Q.Promise<unknown>}
*/
uploadFile(url, file, fieldName) {
let data = new FormData()
data.append(fieldName, new Blob([file]), file.name);
return this.uploadFormData(url, data)
}
/**
* Uploads a form data object.
* @param url
* @param formData
* @returns {Q.Promise<unknown>}
*/
uploadFormData(url, formData) {
const cancel = this.setLoading()
return this.http.put(
url,
formData,
{
headers: {
'Content-Type':
'multipart/form-data; boundary=' + formData._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()
})
}
}

View File

@ -16,8 +16,6 @@ export default class AttachmentService extends AbstractService {
return model
}
uploadProgress = 0
useCreateInterceptor() {
return false
}
@ -61,36 +59,15 @@ export default class AttachmentService extends AbstractService {
* @returns {Promise<any|never>}
*/
create(model, files) {
let data = new FormData()
const 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(
return this.uploadFormData(
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 );
}
}
data
)
.catch(error => {
return this.errorHandler(error)
})
.then(response => {
return Promise.resolve(this.modelCreateFactory(response.data))
})
.finally(() => {
this.uploadProgress = 0
cancel()
})
}
}

View File

@ -8,7 +8,6 @@ export default class BackgroundUploadService extends AbstractService {
})
}
uploadProgress = 0
useCreateInterceptor() {
return false
@ -25,33 +24,10 @@ export default class BackgroundUploadService extends AbstractService {
* @returns {Promise<any|never>}
*/
create(listId, file) {
let data = new FormData()
data.append('background', new Blob([file]), file.name);
const cancel = this.setLoading()
return this.http.put(
return this.uploadFile(
this.getReplacedRoute(this.paths.create, {listId: listId}),
data,
{
headers: {
'Content-Type':
'multipart/form-data; boundary=' + data._boundary,
},
onUploadProgress: progressEvent => {
this.uploadProgress = Math.round((progressEvent.loaded * 100) / progressEvent.total);
}
}
file,
'background'
)
.catch(error => {
return this.errorHandler(error)
})
.then(response => {
return Promise.resolve(this.modelCreateFactory(response.data))
})
.finally(() => {
this.uploadProgress = 0
cancel()
})
}
}