49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
class UploadAdapter {
|
|
constructor(loader, url) {
|
|
this.loader = loader;
|
|
this.url = url;
|
|
}
|
|
|
|
upload() {
|
|
return this.loader.file.then( file => new Promise(((resolve, reject) => {
|
|
this._initRequest();
|
|
this._initListeners( resolve, reject, file );
|
|
this._sendRequest( file );
|
|
})))
|
|
}
|
|
|
|
_initRequest() {
|
|
const xhr = this.xhr = new XMLHttpRequest();
|
|
xhr.open('POST', this.url, true);
|
|
xhr.responseType = 'json';
|
|
}
|
|
|
|
_initListeners(resolve, reject, file) {
|
|
const xhr = this.xhr;
|
|
const loader = this.loader;
|
|
const genericErrorText = '파일을 업로드 할 수 없습니다.'
|
|
|
|
xhr.addEventListener('error', () => {reject(genericErrorText)})
|
|
xhr.addEventListener('abort', () => reject())
|
|
xhr.addEventListener('load', () => {
|
|
const response = xhr.response
|
|
if(!response || response.error) {
|
|
return reject( response && response.error ? response.error.message : genericErrorText );
|
|
}
|
|
|
|
resolve({
|
|
default: response.url
|
|
})
|
|
})
|
|
}
|
|
|
|
_sendRequest(file) {
|
|
const data = new FormData()
|
|
data.append('upload',file)
|
|
this.xhr.send(data)
|
|
}
|
|
}
|
|
function getDownloadUrl(item) {
|
|
return encodeURI(`${API_ROOT_URL}/download/images/${item.linkId}`);
|
|
}
|