2019-07-02 14:38:42 +02:00
|
|
|
import { DirectUpload } from '@rails/activestorage';
|
2020-04-15 16:20:31 +02:00
|
|
|
import { ajax } from '@utils';
|
2019-02-13 14:16:22 +01:00
|
|
|
import ProgressBar from './progress-bar';
|
2020-04-15 16:35:39 +02:00
|
|
|
import FileUploadError, {
|
|
|
|
errorFromDirectUploadMessage,
|
|
|
|
ERROR_CODE_ATTACH
|
|
|
|
} from './file-upload-error';
|
2019-02-13 14:16:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
Uploader class is a delegate for DirectUpload instance
|
2020-01-08 18:15:03 +01:00
|
|
|
used to track lifecycle and progress of an upload.
|
2019-02-13 14:16:22 +01:00
|
|
|
*/
|
|
|
|
export default class Uploader {
|
2022-04-07 17:48:20 +02:00
|
|
|
directUpload: DirectUpload;
|
|
|
|
progressBar: ProgressBar;
|
|
|
|
autoAttachUrl?: string;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
input: HTMLInputElement,
|
|
|
|
file: File,
|
|
|
|
directUploadUrl: string,
|
|
|
|
autoAttachUrl?: string
|
|
|
|
) {
|
2019-02-13 14:16:22 +01:00
|
|
|
this.directUpload = new DirectUpload(file, directUploadUrl, this);
|
2022-04-07 17:48:20 +02:00
|
|
|
this.progressBar = new ProgressBar(input, this.directUpload.id + '', file);
|
2020-04-15 16:20:31 +02:00
|
|
|
this.autoAttachUrl = autoAttachUrl;
|
2019-02-13 14:16:22 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 16:20:31 +02:00
|
|
|
/**
|
|
|
|
Upload (and optionally attach) the file.
|
|
|
|
Returns the blob signed id on success.
|
2020-04-15 16:29:15 +02:00
|
|
|
Throws a FileUploadError on failure.
|
2020-04-15 16:20:31 +02:00
|
|
|
*/
|
|
|
|
async start() {
|
2019-02-13 14:16:22 +01:00
|
|
|
this.progressBar.start();
|
|
|
|
|
2020-04-15 16:20:31 +02:00
|
|
|
try {
|
2022-04-07 17:48:20 +02:00
|
|
|
const blobSignedId = await this._upload();
|
2020-04-15 16:20:31 +02:00
|
|
|
|
|
|
|
if (this.autoAttachUrl) {
|
2022-04-07 17:48:20 +02:00
|
|
|
await this._attach(blobSignedId, this.autoAttachUrl);
|
2020-04-16 17:02:23 +02:00
|
|
|
// On response, the attachment HTML fragment will replace the progress bar.
|
|
|
|
} else {
|
|
|
|
this.progressBar.end();
|
|
|
|
this.progressBar.destroy();
|
2020-04-15 16:20:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return blobSignedId;
|
|
|
|
} catch (error) {
|
2022-04-07 17:48:20 +02:00
|
|
|
this.progressBar.error((error as Error).message);
|
2020-04-15 16:20:31 +02:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Upload the file using the DirectUpload instance, and return the blob signed_id.
|
2020-04-15 16:29:15 +02:00
|
|
|
Throws a FileUploadError on failure.
|
2020-04-15 16:20:31 +02:00
|
|
|
*/
|
2022-04-07 17:48:20 +02:00
|
|
|
async _upload(): Promise<string> {
|
2019-02-13 14:16:22 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-04-09 12:50:47 +02:00
|
|
|
this.directUpload.create((errorMsg, attributes) => {
|
|
|
|
if (errorMsg) {
|
2022-04-24 11:47:17 +02:00
|
|
|
const error = errorFromDirectUploadMessage(errorMsg);
|
2020-04-09 16:45:31 +02:00
|
|
|
reject(error);
|
2019-02-13 14:16:22 +01:00
|
|
|
} else {
|
|
|
|
resolve(attributes.signed_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-15 16:20:31 +02:00
|
|
|
/**
|
|
|
|
Attach the file by sending a POST request to the autoAttachUrl.
|
2020-04-15 16:29:15 +02:00
|
|
|
Throws a FileUploadError on failure (containing the first validation
|
|
|
|
error message, if any).
|
2020-04-15 16:20:31 +02:00
|
|
|
*/
|
2022-04-07 17:48:20 +02:00
|
|
|
async _attach(blobSignedId: string, autoAttachUrl: string) {
|
2020-04-15 16:20:31 +02:00
|
|
|
const attachmentRequest = {
|
2022-04-07 17:48:20 +02:00
|
|
|
url: autoAttachUrl,
|
2020-04-15 16:20:31 +02:00
|
|
|
type: 'PUT',
|
|
|
|
data: `blob_signed_id=${blobSignedId}`
|
|
|
|
};
|
|
|
|
|
2020-04-15 16:29:15 +02:00
|
|
|
try {
|
|
|
|
await ajax(attachmentRequest);
|
|
|
|
} catch (e) {
|
2022-04-07 17:48:20 +02:00
|
|
|
const error = e as {
|
|
|
|
response?: { errors: string[] };
|
|
|
|
xhr?: XMLHttpRequest;
|
|
|
|
};
|
|
|
|
const message = error.response?.errors && error.response.errors[0];
|
2020-04-15 16:29:15 +02:00
|
|
|
throw new FileUploadError(
|
|
|
|
message || 'Error attaching file.',
|
2022-04-07 17:48:20 +02:00
|
|
|
error.xhr?.status,
|
2020-04-15 16:29:15 +02:00
|
|
|
ERROR_CODE_ATTACH
|
|
|
|
);
|
|
|
|
}
|
2020-04-15 16:20:31 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
uploadRequestDidProgress(event: ProgressEvent) {
|
2019-02-13 14:16:22 +01:00
|
|
|
const progress = (event.loaded / event.total) * 100;
|
|
|
|
if (progress) {
|
|
|
|
this.progressBar.progress(progress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:48:20 +02:00
|
|
|
directUploadWillStoreFileWithXHR(xhr: XMLHttpRequest) {
|
2020-04-30 15:42:29 +02:00
|
|
|
xhr.upload.addEventListener('progress', (event) =>
|
2019-02-13 14:16:22 +01:00
|
|
|
this.uploadRequestDidProgress(event)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|