Club System
This commit is contained in:
@@ -61,93 +61,98 @@ export function FSFileData(name, totalChunks, rawFile ) {
|
||||
*/
|
||||
uuid: crypto.randomUUID(),
|
||||
|
||||
/**
|
||||
* Look if this file is currently uploading.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
get isUploading()
|
||||
{
|
||||
return !this.done && !this.error;
|
||||
},
|
||||
|
||||
/**
|
||||
* Build API url.
|
||||
* @param {string} section
|
||||
* @returns {string} The API url.
|
||||
*/
|
||||
buildUrl(section)
|
||||
{
|
||||
return `/api/fs/upload-chunk/${section}`;
|
||||
},
|
||||
|
||||
/**
|
||||
* Upload the file.
|
||||
* @param {string} section section of the file.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async upload(section)
|
||||
{
|
||||
|
||||
if (!this.rawFile)
|
||||
return; // Can't upload in that case.
|
||||
/**
|
||||
* Current file state
|
||||
*/
|
||||
state: 'public',
|
||||
|
||||
/**
|
||||
* Get CSRF token for uploading request.
|
||||
* @type {string}
|
||||
* Look if this file is currently uploading.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
const CSRF = document.querySelector('meta[name=csrf-token]')?.content ?? '';
|
||||
get isUploading()
|
||||
{
|
||||
return !this.done && !this.error;
|
||||
},
|
||||
|
||||
for (let i = 0; i < this.totalChunks; i++) {
|
||||
/**
|
||||
* Build API url.
|
||||
* @param {string} section
|
||||
* @returns {string} The API url.
|
||||
*/
|
||||
buildUrl(section)
|
||||
{
|
||||
return `/api/fs/upload-chunk/${section}`;
|
||||
},
|
||||
|
||||
if (this.error)
|
||||
return; // Abort the process.
|
||||
/**
|
||||
* Upload the file.
|
||||
* @param {string} section section of the file.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async upload(section)
|
||||
{
|
||||
|
||||
const start = i * CHUNK_SIZE;
|
||||
const end = Math.min(start + CHUNK_SIZE, this.rawFile.size);
|
||||
const chunk = this.rawFile.slice(start, end);
|
||||
if (!this.rawFile)
|
||||
return; // Can't upload in that case.
|
||||
|
||||
const formData = new FormData();
|
||||
/**
|
||||
* Get CSRF token for uploading request.
|
||||
* @type {string}
|
||||
*/
|
||||
const CSRF = document.querySelector('meta[name=csrf-token]')?.content ?? '';
|
||||
|
||||
formData.append('file', chunk);
|
||||
formData.append('file_uuid', this.uuid);
|
||||
formData.append('current_chunk', i);
|
||||
formData.append('total_chunks', this.totalChunks);
|
||||
formData.append('filename', this.rawFile.name);
|
||||
formData.append('_token', CSRF);
|
||||
for (let i = 0; i < this.totalChunks; i++) {
|
||||
|
||||
// -----
|
||||
// UPLOAD TIME !
|
||||
// -----
|
||||
if (this.error)
|
||||
return; // Abort the process.
|
||||
|
||||
try {
|
||||
const RESPONSE = await fetch(this.buildUrl(section), {method: 'POST', body: formData});
|
||||
const start = i * CHUNK_SIZE;
|
||||
const end = Math.min(start + CHUNK_SIZE, this.rawFile.size);
|
||||
const chunk = this.rawFile.slice(start, end);
|
||||
|
||||
if (!RESPONSE.ok) // Problem with the request.
|
||||
throw new Error(`${RESPONSE.status} ${RESPONSE.statusText}`);
|
||||
const formData = new FormData();
|
||||
|
||||
/** @type {UploadchunkResponse} */
|
||||
const DATA = await RESPONSE.json();
|
||||
formData.append('file', chunk);
|
||||
formData.append('file_uuid', this.uuid);
|
||||
formData.append('current_chunk', i);
|
||||
formData.append('total_chunks', this.totalChunks);
|
||||
formData.append('filename', this.rawFile.name);
|
||||
formData.append('_token', CSRF);
|
||||
|
||||
if (DATA.success !== true || DATA.uploaded !== true)
|
||||
// The request reached the file server but could not be sent.
|
||||
throw new Error(`${DATA.error}`);
|
||||
// -----
|
||||
// UPLOAD TIME !
|
||||
// -----
|
||||
|
||||
this.currentChunk = i + 1;
|
||||
this.progressValue = Math.round(((i + 1) / this.totalChunks) * 100);
|
||||
try {
|
||||
const RESPONSE = await fetch(this.buildUrl(section), {method: 'POST', body: formData});
|
||||
|
||||
if (DATA.finished === true) {
|
||||
this.done = true;
|
||||
if (!RESPONSE.ok) // Problem with the request.
|
||||
throw new Error(`${RESPONSE.status} ${RESPONSE.statusText}`);
|
||||
|
||||
/** @type {UploadchunkResponse} */
|
||||
const DATA = await RESPONSE.json();
|
||||
|
||||
if (DATA.success !== true || DATA.uploaded !== true)
|
||||
// The request reached the file server but could not be sent.
|
||||
throw new Error(`${DATA.error}`);
|
||||
|
||||
this.currentChunk = i + 1;
|
||||
this.progressValue = Math.round(((i + 1) / this.totalChunks) * 100);
|
||||
|
||||
if (DATA.finished === true) {
|
||||
this.done = true;
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
this.error = 'Error on chunk ' + (i + 1) + '. ' + err.message;
|
||||
this.progressValue = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
this.error = 'Error on chunk ' + (i + 1) + '. ' + err.message;
|
||||
this.progressValue = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user