|
@@ -2,24 +2,9 @@ import DAV from './DAV';
|
|
import Entry from './Entry';
|
|
import Entry from './Entry';
|
|
import State from './State';
|
|
import State from './State';
|
|
import joinPath from './joinPath';
|
|
import joinPath from './joinPath';
|
|
-import { success, error } from 'melba-toast';
|
|
|
|
|
|
+import { success } from 'melba-toast';
|
|
import { t } from 'i18next';
|
|
import { t } from 'i18next';
|
|
|
|
|
|
-const XHRPutFile = (
|
|
|
|
- url: string,
|
|
|
|
- file: File,
|
|
|
|
- onProgress: (progress: number) => void
|
|
|
|
-): Promise<XMLHttpRequest> => {
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- const xhr = new XMLHttpRequest();
|
|
|
|
- xhr.upload.onprogress = (e) => onProgress(e.loaded);
|
|
|
|
- xhr.onloadend = () => resolve(xhr);
|
|
|
|
- xhr.open('PUT', url, true);
|
|
|
|
- xhr.setRequestHeader('Content-Type', file.type);
|
|
|
|
- xhr.send(file);
|
|
|
|
- });
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
export const handleFileUpload = async (
|
|
export const handleFileUpload = async (
|
|
dav: DAV,
|
|
dav: DAV,
|
|
state: State,
|
|
state: State,
|
|
@@ -63,32 +48,12 @@ export const handleFileUpload = async (
|
|
|
|
|
|
collection.add(placeholder);
|
|
collection.add(placeholder);
|
|
|
|
|
|
- const xhr = await XHRPutFile(
|
|
|
|
- joinPath(location.pathname, file.name),
|
|
|
|
- file,
|
|
|
|
- (uploaded: number) => {
|
|
|
|
- placeholder.uploadedSize = uploaded;
|
|
|
|
- placeholder.emit('updated');
|
|
|
|
- }
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
- const ok = xhr.status >= 200 && xhr.status < 300;
|
|
|
|
|
|
+ const result = await dav.upload(location.pathname, file);
|
|
|
|
|
|
- if (!ok) {
|
|
|
|
|
|
+ if (!result.ok) {
|
|
collection.remove(placeholder);
|
|
collection.remove(placeholder);
|
|
- state.update();
|
|
|
|
|
|
|
|
- error(
|
|
|
|
- t('failure', {
|
|
|
|
- interpolation: {
|
|
|
|
- escapeValue: false,
|
|
|
|
- },
|
|
|
|
- method: 'PUT',
|
|
|
|
- url: xhr.responseURL,
|
|
|
|
- statusText: xhr.statusText,
|
|
|
|
- status: xhr.status,
|
|
|
|
- })
|
|
|
|
- );
|
|
|
|
|
|
+ state.update();
|
|
|
|
|
|
return;
|
|
return;
|
|
}
|
|
}
|