Continue Upload Script.

Pause during rewrite of RHPZ Server code.
This commit is contained in:
2026-01-29 18:49:47 +01:00
parent f029371a68
commit f34019c802
9 changed files with 210 additions and 7 deletions

View File

@@ -1,4 +1,12 @@
{ {
"name": "romhackplaza/wp-plugin",
"type": "project",
"authors": [
{
"name": "Benjamin",
"email": "benjamin@cartememoire.org"
}
],
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"RomhackPlaza\\": "src/" "RomhackPlaza\\": "src/"

View File

@@ -111,11 +111,15 @@ class Submissions extends Abstract_Extender {
'submissions-uploader', 'submissions-uploader',
ROMHACKPLAZA_PLUGIN_URI . '/assets/js/submissions/uploader.js', ROMHACKPLAZA_PLUGIN_URI . '/assets/js/submissions/uploader.js',
[], [],
'20251106-68', '20251106-77',
args: [ 'defer' => true, 'in_footer' => true ] args: [ 'defer' => true, 'in_footer' => true ]
)->enqueue()->add_localize( )->enqueue()->add_localize(
'_romhackplaza_script_uploader', '_romhackplaza_script_uploader',
[ 'submit_url' => admin_url( 'admin-ajax.php' ) ], [
'submit_url' => admin_url( 'admin-ajax.php' ),
'reserve_nonce' => wp_create_nonce( Format::format_nonce_name( 'reserve_post_id' ) ),
'set_favorite_nonce' => wp_create_nonce( Format::format_nonce_name( 'set_favorite_server' ) ),
],
)->enable_i18n(); )->enable_i18n();
} }

View File

@@ -14,5 +14,41 @@ export class Reserve_Post_ID {
callback(); callback();
return; 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" ), "" );
}
});
} }
} }

View 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 );
});
}
}

View 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" );
}
});
}
}

View File

@@ -1,5 +1,7 @@
import {__, FORBIDDEN_CARS, I } from "./globals"; import {__, FORBIDDEN_CARS, I, API } from "./globals";
import {DropContainer} from "./class-drop-container"; 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_script_uploader: any;
declare const romhackplaza_modal_submissions: any; declare const romhackplaza_modal_submissions: any;
@@ -27,6 +29,7 @@ export class Upload {
} }
this.switchDuringUpload(); this.switchDuringUpload();
// @ts-ignore
if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer ) if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer )
I.drop_container.switch(); I.drop_container.switch();
@@ -36,8 +39,56 @@ export class Upload {
this.progress_bar = progress.querySelector('.bar') as HTMLElement; this.progress_bar = progress.querySelector('.bar') as HTMLElement;
} }
if( I.reserved_post_id === undefined || I.reserved_post_id === null ) if( I.reserved_post_id === undefined || I.reserved_post_id === null ){
console.log( "ok" ); // 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 = () => {
} }

View File

@@ -1,21 +1,31 @@
export class Uploader_Data { export class Uploader_Data {
[x: string]: any // @ts-ignore
;
// @ts-ignore
private data: object; private data: object;
constructor() { constructor() {
this.data = {}; this.data = {};
return new Proxy( this, { 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 ); 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 ); target.set( p as string, v );
return true return true
} }
}); });
} }
// @ts-ignore
add_field( field_name: string, field_value: any, readonly: boolean ){ add_field( field_name: string, field_value: any, readonly: boolean ){
// @ts-ignore // @ts-ignore
@@ -25,6 +35,7 @@ export class Uploader_Data {
} }
// @ts-ignore
get( field_name : string ): any { get( field_name : string ): any {
// @ts-ignore // @ts-ignore
@@ -36,6 +47,7 @@ export class Uploader_Data {
} }
// @ts-ignore
set( field_name: string, field_value: any ){ set( field_name: string, field_value: any ){
// @ts-ignore // @ts-ignore

View File

@@ -1,4 +1,5 @@
import {Uploader_Data} from "./class-uploader-data"; import {Uploader_Data} from "./class-uploader-data";
import {Server_Api} from "./class-server-api";
export const { __, _x, _n, _nx } = wp.i18n; 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(); export const I : Uploader_Data = new Uploader_Data();
I.add_field( 'is_edit', false, true ); 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( 'reserved_post_id', undefined, false );
I.add_field( 'during_upload', false, false ); I.add_field( 'during_upload', false, false );
I.add_field( 'upload', undefined, false ); I.add_field( 'upload', undefined, false );
I.add_field( 'drop_container', undefined, true ); 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 { export interface Can_Upload_Detail {
file: File; file: File;

View File

@@ -9,7 +9,10 @@ document.addEventListener('DOMContentLoaded', () => {
// Add PostID if edit. // Add PostID if edit.
let url_params = new URLSearchParams(window.location.search); let url_params = new URLSearchParams(window.location.search);
// @ts-ignore
I.is_edit = url_params.has( 'edit_entry' ); 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; I.reserved_post_id = url_params.get('edit_entry' ) || null;
// @ts-ignore // @ts-ignore