Continue Upload Script.
Pause during rewrite of RHPZ Server code.
This commit is contained in:
@@ -14,5 +14,41 @@ export class Reserve_Post_ID {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
if( I.custom_post_type === undefined || I.custom_post_type === "unknown" ){
|
||||
console.error( "The custom post type is missing" );
|
||||
return;
|
||||
}
|
||||
|
||||
this.do_request( callback );
|
||||
|
||||
}
|
||||
|
||||
private do_request( callback: () => any ){
|
||||
|
||||
jQuery.ajax({
|
||||
type: "POST",
|
||||
url: _romhackplaza_script_uploader.submit_url,
|
||||
data: {
|
||||
action: "reserve_post_id",
|
||||
custom_post_type: I.custom_post_type,
|
||||
_wpnonce: _romhackplaza_script_uploader.reserve_nonce
|
||||
},
|
||||
success: function( response: any ) {
|
||||
if( response.success ) {
|
||||
|
||||
I.reserved_post_id = response.data.post_id;
|
||||
jQuery( "#reservedPostID" ).val( I.reserved_post_id );
|
||||
callback();
|
||||
|
||||
} else {
|
||||
romhackplaza_manage_modal( romhackplaza_modal_submissions, "block", __( "Submit error", 'romhackplaza' ), __( "Could not reserve a post ID. Please refresh the page.", "romhackplaza" ), "" );
|
||||
}
|
||||
},
|
||||
error: function( response: any ) {
|
||||
romhackplaza_manage_modal( romhackplaza_modal_submissions, "block", __( "Submit error", 'romhackplaza' ), __( "Error occurred while reserving a post ID. Please refresh the page.", "romhackplaza" ), "" );
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
50
ts/submissions/class-server-api.ts
Normal file
50
ts/submissions/class-server-api.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { __, I } from "./globals";
|
||||
|
||||
export class Server_Api {
|
||||
|
||||
url: string|undefined;
|
||||
id: number|undefined;
|
||||
|
||||
init( server_url: string, server_id: number ){
|
||||
this.url = server_url;
|
||||
this.id = server_id;
|
||||
}
|
||||
|
||||
is_init(): boolean {
|
||||
return this.url !== undefined && this.id !== undefined;
|
||||
}
|
||||
|
||||
private build_url( call: string ): string {
|
||||
|
||||
let new_url: string = this.url + "upload.php";
|
||||
if( call === "check_existence" )
|
||||
new_url += "?checkExistence=true";
|
||||
|
||||
return new_url;
|
||||
}
|
||||
|
||||
async check_file_existence( filename: string ): Promise<boolean> {
|
||||
|
||||
return new Promise( ( resolve, reject ) => {
|
||||
|
||||
const XHR: XMLHttpRequest = new XMLHttpRequest();
|
||||
XHR.open( 'POST', this.build_url( 'check_existence' ), true );
|
||||
|
||||
let form_data: FormData = new FormData();
|
||||
form_data.append( "filename", filename );
|
||||
form_data.append( 'reservedPostID', I.reserved_post_id );
|
||||
form_data.append( 'custom_post_type', I.custom_post_type );
|
||||
|
||||
XHR.onload = () => {
|
||||
resolve( XHR.responseText === 'exists' );
|
||||
}
|
||||
XHR.onerror = () => {
|
||||
reject( false );
|
||||
}
|
||||
|
||||
XHR.send( form_data );
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
34
ts/submissions/class-set-favorite-server.ts
Normal file
34
ts/submissions/class-set-favorite-server.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import {__, I } from "./globals";
|
||||
declare const _romhackplaza_script_uploader: any;
|
||||
declare const romhackplaza_modal_submissions: any;
|
||||
|
||||
export class Set_Favorite_Server {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param callback - MUST BE FROM A Upload child.
|
||||
*/
|
||||
constructor( server_id: number ){
|
||||
|
||||
this.do_request( server_id );
|
||||
|
||||
}
|
||||
|
||||
private do_request( server_id : number ){
|
||||
|
||||
jQuery.ajax({
|
||||
type: "POST",
|
||||
url: _romhackplaza_script_uploader.submit_url,
|
||||
data: {
|
||||
action: 'set_favorite_server',
|
||||
post_id: I.reserved_post_id,
|
||||
favorite_server: server_id,
|
||||
_wpnonce: _romhackplaza_script_uploader.set_favorite_nonce
|
||||
},
|
||||
success: function(){
|
||||
console.log( "Favorite server set" );
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import {__, FORBIDDEN_CARS, I } from "./globals";
|
||||
import {__, FORBIDDEN_CARS, I, API } from "./globals";
|
||||
import {DropContainer} from "./class-drop-container";
|
||||
import {Reserve_Post_ID} from "./class-reserve-post-id";
|
||||
import {Set_Favorite_Server} from "./class-set-favorite-server";
|
||||
declare const _romhackplaza_script_uploader: any;
|
||||
declare const romhackplaza_modal_submissions: any;
|
||||
|
||||
@@ -27,6 +29,7 @@ export class Upload {
|
||||
}
|
||||
|
||||
this.switchDuringUpload();
|
||||
// @ts-ignore
|
||||
if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer )
|
||||
I.drop_container.switch();
|
||||
|
||||
@@ -36,8 +39,56 @@ export class Upload {
|
||||
this.progress_bar = progress.querySelector('.bar') as HTMLElement;
|
||||
}
|
||||
|
||||
if( I.reserved_post_id === undefined || I.reserved_post_id === null )
|
||||
console.log( "ok" );
|
||||
if( I.reserved_post_id === undefined || I.reserved_post_id === null ){
|
||||
// Reserve a post ID.
|
||||
new Reserve_Post_ID( this.step1_checkFileExists );
|
||||
} else
|
||||
// Already reserved.
|
||||
this.step1_checkFileExists();
|
||||
|
||||
}
|
||||
|
||||
step1_checkFileExists = () => {
|
||||
|
||||
if( I.favorite_server_set === false ){
|
||||
// @ts-ignore
|
||||
new Set_Favorite_Server( chosenDownloadServerId || 0 );
|
||||
I.favorite_server_set = true;
|
||||
}
|
||||
|
||||
API.check_file_existence( this.file.name ).then( exists => {
|
||||
|
||||
if( !exists ){
|
||||
this.step3_uploadFile();
|
||||
// @ts-ignore
|
||||
document.getElementById( 'cancelButton' )?.style.display = 'block';
|
||||
} else {
|
||||
|
||||
let want_to_delete = window.confirm( __( "File already exists! Proceed with upload and overwrite it ?" ) );
|
||||
if( want_to_delete )
|
||||
this.step2_deleteFileBeforeUpload();
|
||||
|
||||
else{
|
||||
|
||||
// @ts-ignore
|
||||
if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer )
|
||||
I.drop_container.switch();
|
||||
this.switchDuringUpload();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
step2_deleteFileBeforeUpload = () => {
|
||||
|
||||
|
||||
}
|
||||
|
||||
step3_uploadFile = () => {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,31 @@
|
||||
export class Uploader_Data {
|
||||
[x: string]: any // @ts-ignore
|
||||
;
|
||||
|
||||
// @ts-ignore
|
||||
private data: object;
|
||||
|
||||
constructor() {
|
||||
this.data = {};
|
||||
|
||||
return new Proxy( this, {
|
||||
get: ( target, p ) => {
|
||||
get: ( target, p, receiver ) => {
|
||||
if (p in target) {
|
||||
return Reflect.get(target, p, receiver);
|
||||
}
|
||||
return target.get( p as string );
|
||||
},
|
||||
set: ( target, p, v ) => {
|
||||
set: ( target, p, v , receiver) => {
|
||||
if (p in target) {
|
||||
return Reflect.set(target, p, v, receiver);
|
||||
}
|
||||
target.set( p as string, v );
|
||||
return true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
add_field( field_name: string, field_value: any, readonly: boolean ){
|
||||
|
||||
// @ts-ignore
|
||||
@@ -25,6 +35,7 @@ export class Uploader_Data {
|
||||
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
get( field_name : string ): any {
|
||||
|
||||
// @ts-ignore
|
||||
@@ -36,6 +47,7 @@ export class Uploader_Data {
|
||||
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
set( field_name: string, field_value: any ){
|
||||
|
||||
// @ts-ignore
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {Uploader_Data} from "./class-uploader-data";
|
||||
import {Server_Api} from "./class-server-api";
|
||||
|
||||
export const { __, _x, _n, _nx } = wp.i18n;
|
||||
|
||||
@@ -8,10 +9,14 @@ export const FORBIDDEN_CARS: Array<string> = [
|
||||
|
||||
export const I : Uploader_Data = new Uploader_Data();
|
||||
I.add_field( 'is_edit', false, true );
|
||||
I.add_field( 'custom_post_type', undefined, true );
|
||||
I.add_field( 'reserved_post_id', undefined, false );
|
||||
I.add_field( 'during_upload', false, false );
|
||||
I.add_field( 'upload', undefined, false );
|
||||
I.add_field( 'drop_container', undefined, true );
|
||||
I.add_field( 'favorite_server_set', false, false );
|
||||
|
||||
export const API : Server_Api = new Server_Api();
|
||||
|
||||
export interface Can_Upload_Detail {
|
||||
file: File;
|
||||
|
||||
@@ -9,7 +9,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
// Add PostID if edit.
|
||||
let url_params = new URLSearchParams(window.location.search);
|
||||
// @ts-ignore
|
||||
I.is_edit = url_params.has( 'edit_entry' );
|
||||
|
||||
I.custom_post_type = jQuery( 'input[name="custom_post_type"]').val() || 'unknown';
|
||||
I.reserved_post_id = url_params.get('edit_entry' ) || null;
|
||||
|
||||
// @ts-ignore
|
||||
|
||||
Reference in New Issue
Block a user