Переглянути джерело

qdr-temporary-shared-storage: rename "media_id" to "fileid" and change type from bigint to string (size 50 chars). "fileid" value is alphanumeric unique hash (generated from timestamp and id) with size 50 chars.

Dalibor Votruba 1 рік тому
батько
коміт
998a2b58d4

+ 2 - 2
qdr-temporary-shared-storage/admin/class-qdr-tss-media-page.php

@@ -214,8 +214,8 @@ class QDR_TSS_Media_Page {
         }
         
         // Add permalink and shortcode
-        $item->permalink = site_url('shared-file-download/' . $item->media_id);
-        $item->shortcode = '[qdr_tss_download id="' . $item->media_id . '"]';
+        $item->permalink = site_url('shared-file-download/' . $item->fileid);
+        $item->shortcode = '[qdr_tss_download id="' . $item->fileid . '"]';
         
         // Remove password from response
         unset($item->password);

+ 2 - 2
qdr-temporary-shared-storage/admin/js/admin.js

@@ -211,7 +211,7 @@
                 
                 var row = '<tr data-id="' + item.id + '" class="' + statusClass + '">';
                 
-                row += '<td>' + item.media_id + '</td>';
+                row += '<td>' + item.fileid + '</td>';
                 row += '<td>' + self.escapeHtml(item.original_file_name) + '</td>';
                 row += '<td>' + self.escapeHtml(item.description || '') + '</td>';
                 row += '<td>' + self.escapeHtml(item.message || '') + '</td>';
@@ -356,7 +356,7 @@
                     var item = response.data.item;
                     
                     // Populate details
-                    $('#qdr-tss-details-media-id').text(item.media_id);
+                    $('#qdr-tss-details-fileid').text(item.fileid);
                     $('#qdr-tss-details-filename').text(item.original_file_name);
                     $('#qdr-tss-details-description').text(item.description || '-');
                     $('#qdr-tss-details-message').text(item.message || '-');

+ 3 - 3
qdr-temporary-shared-storage/admin/partials/media-page.php

@@ -56,7 +56,7 @@ $usage_percent = ($max_size > 0) ? min(100, ($total_size / $max_size) * 100) : 0
         <table class="wp-list-table widefat fixed striped qdr-tss-table">
             <thead>
                 <tr>
-                    <th class="column-media-id sortable" data-sort="media_id"><?php esc_html_e('Media ID', 'qdr-temporary-shared-storage'); ?> <span class="sorting-indicator"></span></th>
+                    <th class="column-fileid sortable" data-sort="fileid"><?php esc_html_e('File ID', 'qdr-temporary-shared-storage'); ?> <span class="sorting-indicator"></span></th>
                     <th class="column-filename sortable" data-sort="original_file_name"><?php esc_html_e('File Name', 'qdr-temporary-shared-storage'); ?> <span class="sorting-indicator"></span></th>
                     <th class="column-description"><?php esc_html_e('Description', 'qdr-temporary-shared-storage'); ?></th>
                     <th class="column-message"><?php esc_html_e('Message', 'qdr-temporary-shared-storage'); ?></th>
@@ -138,8 +138,8 @@ $usage_percent = ($max_size > 0) ? min(100, ($total_size / $max_size) * 100) : 0
         <div class="qdr-tss-details-content">
             <table class="widefat striped">
                 <tr>
-                    <th><?php esc_html_e('Media ID', 'qdr-temporary-shared-storage'); ?></th>
-                    <td id="qdr-tss-details-media-id"></td>
+                    <th><?php esc_html_e('File ID', 'qdr-temporary-shared-storage'); ?></th>
+                    <td id="qdr-tss-details-fileid"></td>
                 </tr>
                 <tr>
                     <th><?php esc_html_e('File Name', 'qdr-temporary-shared-storage'); ?></th>

+ 19 - 19
qdr-temporary-shared-storage/includes/class-qdr-tss-db-manager.php

@@ -63,7 +63,7 @@ class QDR_TSS_DB_Manager {
         
         $sql = "CREATE TABLE $this->table_name (
             id bigint(20) NOT NULL AUTO_INCREMENT,
-            media_id bigint(20) NOT NULL,
+            fileid varchar(50) NOT NULL,
             original_file_name varchar(255) NOT NULL,
             description text NULL,
             message text NULL,
@@ -77,7 +77,7 @@ class QDR_TSS_DB_Manager {
             file_path varchar(255) NOT NULL,
             file_size bigint(20) NOT NULL,
             PRIMARY KEY  (id),
-            KEY media_id (media_id),
+            KEY fileid (fileid),
             KEY reference (reference),
             KEY active_to (active_to)
         ) $charset_collate;";
@@ -100,16 +100,16 @@ class QDR_TSS_DB_Manager {
     }
 
     /**
-     * Get an item by media ID.
+     * Get an item by fileid.
      *
      * @since 1.0.0
-     * @param int $media_id The media ID.
+     * @param string $fileid The file ID.
      * @return object|null Database row as object or null if not found.
      */
-    public function get_item_by_media_id($media_id) {
+    public function get_item_by_fileid($fileid) {
         global $wpdb;
         
-        return $wpdb->get_row($wpdb->prepare("SELECT * FROM $this->table_name WHERE media_id = %d", $media_id));
+        return $wpdb->get_row($wpdb->prepare("SELECT * FROM $this->table_name WHERE fileid = %s", $fileid));
     }
 
     /**
@@ -154,7 +154,7 @@ class QDR_TSS_DB_Manager {
         
         // Allowed columns for orderby
         $allowed_orderby_columns = array(
-            'media_id', 'original_file_name', 'description', 'active_from', 'active_to',
+            'fileid', 'original_file_name', 'description', 'active_from', 'active_to',
             'reference', 'upload_date', 'download_count', 'last_download'
         );
         
@@ -250,22 +250,22 @@ class QDR_TSS_DB_Manager {
     }
 
     /**
-     * Update an item by media ID.
+     * Update an item by fileid.
      *
      * @since 1.0.0
-     * @param int   $media_id The media ID.
-     * @param array $data     The item data.
+     * @param string $fileid The file ID.
+     * @param array  $data   The item data.
      * @return bool True on success, false on failure.
      */
-    public function update_item_by_media_id($media_id, $data) {
+    public function update_item_by_fileid($fileid, $data) {
         global $wpdb;
         
         return $wpdb->update(
             $this->table_name,
             $data,
-            array('media_id' => $media_id),
+            array('fileid' => $fileid),
             $this->get_column_formats($data),
-            array('%d')
+            array('%s')
         );
     }
 
@@ -287,19 +287,19 @@ class QDR_TSS_DB_Manager {
     }
 
     /**
-     * Delete an item by media ID.
+     * Delete an item by fileid.
      *
      * @since 1.0.0
-     * @param int $media_id The media ID.
+     * @param string $fileid The file ID.
      * @return bool True on success, false on failure.
      */
-    public function delete_item_by_media_id($media_id) {
+    public function delete_item_by_fileid($fileid) {
         global $wpdb;
         
         return $wpdb->delete(
             $this->table_name,
-            array('media_id' => $media_id),
-            array('%d')
+            array('fileid' => $fileid),
+            array('%s')
         );
     }
 
@@ -395,7 +395,7 @@ class QDR_TSS_DB_Manager {
         
         $columns = array(
             'id' => '%d',
-            'media_id' => '%d',
+            'fileid' => '%s',
             'original_file_name' => '%s',
             'description' => '%s',
             'message' => '%s',

+ 19 - 0
qdr-temporary-shared-storage/includes/class-qdr-tss-file-manager.php

@@ -42,6 +42,25 @@ class QDR_TSS_File_Manager {
         $this->init_file_system();
     }
 
+    /**
+     * Generate a unique file ID.
+     *
+     * @since 1.0.0
+     * @return string A unique alphanumeric hash
+     */
+    public function generate_fileid() {
+        // Current timestamp + random string
+        $timestamp = time();
+        $random = bin2hex(random_bytes(10)); // 20 chars
+        $unique = uniqid('', true); // ~23 chars
+        
+        // Create hash from combined values
+        $hash = md5($timestamp . $random . $unique);
+        
+        // Return first 40 chars of the hash + timestamp hex (should be under 50 chars total)
+        return substr($hash, 0, 40) . dechex($timestamp);
+    }
+
     /**
      * Initialize file system directories.
      *

+ 7 - 4
qdr-temporary-shared-storage/instructions.txt

@@ -11,17 +11,20 @@ Plugin will have following features:
 	password - password for download file (string)
 	reference - Custom reference value (string)
 	
+	- unique fileid will be generated
+	- file will be saved inside repository as fileid
+
 	- returns:
-		media_id
+		fileid (string) - auto generated alphanumeric unique hash
 		permalink (or shortcode) to download file
 	
-2. Allows to edit folowing media properties by media_id via REST API:
+2. Allows to edit folowing media properties by fileid via REST API:
 	message - Custom message (string)
 	active-from - Datetime from permalink will be active (datetime)
 	active-to - Datetime to permalink will be active (datetime) (datetime)
 	reference - Custom reference value (string)
 
-3. Allows to delete media by media_id via REST API
+3. Allows to delete media by fileid via REST API
 4. Allows to get media following properties via REST API:
 	original-file-name - Original file name (string)
 	description - File description (string)
@@ -45,7 +48,7 @@ Plugin will have following features:
 
 5. Creates Cron job (one per hour) to scan for expired (active-to) media (media record and content files) and deletes them
 6. Creates in menu Media | Shared Storage page with grid that will have following columns:
-	media_id
+	fileid
 	original-file-name - Original file name (string)
 	description - File description (string)
 	message - Custom message (string)

+ 69 - 0
qdr-temporary-shared-storage/pack.cmd

@@ -0,0 +1,69 @@
+@echo off
+setlocal enabledelayedexpansion
+
+echo QDR Temporary Shared Storage Plugin Packager
+echo ===========================================
+echo.
+
+:: Set variables
+set PLUGIN_NAME=qdr-temporary-shared-storage
+set DEPLOY_DIR=D:\Deploy
+set CURRENT_DIR=%CD%
+set TARGET_ZIP=%DEPLOY_DIR%\%PLUGIN_NAME%.zip
+set SCRIPT_NAME=%~nx0
+
+:: Create deploy directory if it doesn't exist
+if not exist "%DEPLOY_DIR%" (
+    echo Creating deploy directory %DEPLOY_DIR%...
+    mkdir "%DEPLOY_DIR%"
+)
+
+:: Remove existing ZIP if it exists
+if exist "%TARGET_ZIP%" (
+    echo Removing existing archive...
+    del "%TARGET_ZIP%"
+)
+
+:: Create temporary directory for files to include
+set TEMP_DIR=%TEMP%\%PLUGIN_NAME%_package
+if exist "%TEMP_DIR%" rmdir /s /q "%TEMP_DIR%"
+mkdir "%TEMP_DIR%"
+
+echo Copying plugin files to temporary directory...
+
+:: Copy all files and directories except excluded ones
+for /d %%D in (*) do (
+    if /i not "%%D"=="@documentation" (
+        xcopy "%%D" "%TEMP_DIR%\%%D\" /E /I /H /Y >nul
+    )
+)
+
+:: Copy individual files except the script itself
+for %%F in (*.*) do (
+    if /i not "%%F"=="%SCRIPT_NAME%" (
+        copy "%%F" "%TEMP_DIR%\" >nul
+    )
+)
+
+echo Creating ZIP archive...
+
+:: Create ZIP file using PowerShell
+powershell -Command "Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::CreateFromDirectory('%TEMP_DIR%', '%TARGET_ZIP%')"
+
+:: Clean up temporary directory
+rmdir /s /q "%TEMP_DIR%"
+
+:: Verify creation and display result
+if exist "%TARGET_ZIP%" (
+    echo.
+    echo Package created successfully: %TARGET_ZIP%
+    echo Size: 
+    for %%A in ("%TARGET_ZIP%") do echo %%~zA bytes
+) else (
+    echo.
+    echo ERROR: Failed to create package!
+)
+
+echo.
+echo Process complete.
+endlocal

+ 19 - 19
qdr-temporary-shared-storage/public/class-qdr-tss-download.php

@@ -70,19 +70,19 @@ class QDR_TSS_Download {
             'id' => null,
         ), $atts, 'qdr_tss_download');
         
-        // Get media ID from shortcode attribute or URL parameter
-        $media_id = $atts['id'];
-        if (!$media_id && isset($_GET['id'])) {
-            $media_id = intval($_GET['id']);
+        // Get fileid from shortcode attribute or URL parameter
+        $fileid = $atts['id'];
+        if (!$fileid && isset($_GET['id'])) {
+            $fileid = sanitize_text_field($_GET['id']);
         }
         
-        // If no media ID, show form to enter media ID, password and reference
-        if (!$media_id) {
+        // If no fileid, show form to enter fileid, password and reference
+        if (!$fileid) {
             return $this->render_download_form();
         }
         
-        // Get media information
-        $item = $this->db_manager->get_item_by_media_id($media_id);
+        // Get file information
+        $item = $this->db_manager->get_item_by_fileid($fileid);
         
         if (!$item) {
             return '<div class="qdr-tss-error">' . __('File not found.', 'qdr-temporary-shared-storage') . '</div>';
@@ -108,14 +108,14 @@ class QDR_TSS_Download {
             // Verify password
             $password = isset($_POST['qdr_tss_password']) ? $_POST['qdr_tss_password'] : '';
             if (!wp_check_password($password, $item->password)) {
-                return '<div class="qdr-tss-error">' . __('Invalid password.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($media_id);
+                return '<div class="qdr-tss-error">' . __('Invalid password.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($fileid);
             }
             
             // Verify reference if set
             if (!empty($item->reference)) {
                 $reference = isset($_POST['qdr_tss_reference']) ? $_POST['qdr_tss_reference'] : '';
                 if ($reference !== $item->reference) {
-                    return '<div class="qdr-tss-error">' . __('Invalid reference.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($media_id);
+                    return '<div class="qdr-tss-error">' . __('Invalid reference.', 'qdr-temporary-shared-storage') . '</div>' . $this->render_download_form($fileid);
                 }
             }
             
@@ -143,7 +143,7 @@ class QDR_TSS_Download {
             }
         }
         
-        // Show download form with media information
+        // Show download form with file information
         $output = '<div class="qdr-tss-download-info">';
         $output .= '<h2>' . __('File Information', 'qdr-temporary-shared-storage') . '</h2>';
         $output .= '<p><strong>' . __('File Name:', 'qdr-temporary-shared-storage') . '</strong> ' . esc_html($item->original_file_name) . '</p>';
@@ -157,7 +157,7 @@ class QDR_TSS_Download {
         }
         
         $output .= '</div>';
-        $output .= $this->render_download_form($media_id);
+        $output .= $this->render_download_form($fileid);
         
         return $output;
     }
@@ -166,23 +166,23 @@ class QDR_TSS_Download {
      * Render download form.
      *
      * @since    1.0.0
-     * @param    int       $media_id    Optional. Media ID.
-     * @return   string                 Download form HTML.
+     * @param    string    $fileid    Optional. File ID.
+     * @return   string               Download form HTML.
      */
-    private function render_download_form($media_id = null) {
+    private function render_download_form($fileid = null) {
         $form = '<div class="qdr-tss-download-form">';
         $form .= '<form method="post" action="">';
         
         // Add nonce for security
         $form .= wp_nonce_field('qdr_tss_download', 'qdr_tss_nonce', true, false);
         
-        if (!$media_id) {
+        if (!$fileid) {
             $form .= '<div class="qdr-tss-form-group">';
-            $form .= '<label for="qdr_tss_media_id">' . __('Media ID', 'qdr-temporary-shared-storage') . '</label>';
-            $form .= '<input type="text" name="id" id="qdr_tss_media_id" required>';
+            $form .= '<label for="qdr_tss_fileid">' . __('File ID', 'qdr-temporary-shared-storage') . '</label>';
+            $form .= '<input type="text" name="id" id="qdr_tss_fileid" required>';
             $form .= '</div>';
         } else {
-            $form .= '<input type="hidden" name="id" value="' . esc_attr($media_id) . '">';
+            $form .= '<input type="hidden" name="id" value="' . esc_attr($fileid) . '">';
         }
         
         $form .= '<div class="qdr-tss-form-group">';

+ 16 - 16
qdr-temporary-shared-storage/rest-api/class-qdr-tss-media-controller.php

@@ -91,7 +91,7 @@ class QDR_TSS_Media_Controller {
             ),
         ));
         
-        register_rest_route($this->namespace, '/' . $this->base . '/(?P<id>[\d]+)', array(
+        register_rest_route($this->namespace, '/' . $this->base . '/(?P<id>[\w]+)', array(
             array(
                 'methods' => WP_REST_Server::READABLE,
                 'callback' => array($this, 'get_item'),
@@ -155,14 +155,14 @@ class QDR_TSS_Media_Controller {
      * @return   WP_REST_Response|WP_Error      Response object on success, or WP_Error object on failure.
      */
     public function get_item($request) {
-        $id = (int) $request['id'];
+        $id = $request['id'];
         
-        $item = $this->db_manager->get_item_by_media_id($id);
+        $item = $this->db_manager->get_item_by_fileid($id);
         
         if (!$item) {
             return new WP_Error(
                 'qdr_tss_not_found',
-                __('Media not found.', 'qdr-temporary-shared-storage'),
+                __('File not found.', 'qdr-temporary-shared-storage'),
                 array('status' => 404)
             );
         }
@@ -181,14 +181,14 @@ class QDR_TSS_Media_Controller {
      * @return   WP_REST_Response|WP_Error      Response object on success, or WP_Error object on failure.
      */
     public function update_item($request) {
-        $id = (int) $request['id'];
+        $id = $request['id'];
         
-        $item = $this->db_manager->get_item_by_media_id($id);
+        $item = $this->db_manager->get_item_by_fileid($id);
         
         if (!$item) {
             return new WP_Error(
                 'qdr_tss_not_found',
-                __('Media not found.', 'qdr-temporary-shared-storage'),
+                __('File not found.', 'qdr-temporary-shared-storage'),
                 array('status' => 404)
             );
         }
@@ -224,17 +224,17 @@ class QDR_TSS_Media_Controller {
             );
         }
         
-        $result = $this->db_manager->update_item_by_media_id($id, $data);
+        $result = $this->db_manager->update_item_by_fileid($id, $data);
         
         if (!$result) {
             return new WP_Error(
                 'qdr_tss_update_failed',
-                __('Failed to update media.', 'qdr-temporary-shared-storage'),
+                __('Failed to update file.', 'qdr-temporary-shared-storage'),
                 array('status' => 500)
             );
         }
         
-        $updated_item = $this->db_manager->get_item_by_media_id($id);
+        $updated_item = $this->db_manager->get_item_by_fileid($id);
         
         // Remove password from response
         unset($updated_item->password);
@@ -250,14 +250,14 @@ class QDR_TSS_Media_Controller {
      * @return   WP_REST_Response|WP_Error      Response object on success, or WP_Error object on failure.
      */
     public function delete_item($request) {
-        $id = (int) $request['id'];
+        $id = $request['id'];
         
-        $item = $this->db_manager->get_item_by_media_id($id);
+        $item = $this->db_manager->get_item_by_fileid($id);
         
         if (!$item) {
             return new WP_Error(
                 'qdr_tss_not_found',
-                __('Media not found.', 'qdr-temporary-shared-storage'),
+                __('File not found.', 'qdr-temporary-shared-storage'),
                 array('status' => 404)
             );
         }
@@ -266,19 +266,19 @@ class QDR_TSS_Media_Controller {
         $this->file_manager->delete_file($item->file_path);
         
         // Delete from database
-        $result = $this->db_manager->delete_item_by_media_id($id);
+        $result = $this->db_manager->delete_item_by_fileid($id);
         
         if (!$result) {
             return new WP_Error(
                 'qdr_tss_delete_failed',
-                __('Failed to delete media.', 'qdr-temporary-shared-storage'),
+                __('Failed to delete file.', 'qdr-temporary-shared-storage'),
                 array('status' => 500)
             );
         }
         
         return new WP_REST_Response(array(
             'deleted' => true,
-            'media_id' => $id
+            'fileid' => $id
         ), 200);
     }
 }

+ 6 - 6
qdr-temporary-shared-storage/rest-api/class-qdr-tss-upload-controller.php

@@ -285,12 +285,12 @@ class QDR_TSS_Upload_Controller {
         $active_from = !empty($request['active_from']) ? $request['active_from'] : current_time('mysql');
         $active_to = !empty($request['active_to']) ? $request['active_to'] : date('Y-m-d H:i:s', strtotime('+30 days'));
         
-        // Generate a unique media ID
-        $media_id = time() . rand(1000, 9999);
+        // Generate a unique fileid
+        $fileid = $this->file_manager->generate_fileid();
         
         // Insert into database
         $data = array(
-            'media_id' => $media_id,
+            'fileid' => $fileid,
             'original_file_name' => sanitize_file_name($request['original_file_name']),
             'description' => sanitize_textarea_field($request['description']),
             'message' => isset($request['message']) ? sanitize_textarea_field($request['message']) : '',
@@ -316,11 +316,11 @@ class QDR_TSS_Upload_Controller {
             );
         }
         
-        $download_url = site_url('shared-file-download/' . $media_id);
-        $shortcode = '[qdr_tss_download id="' . $media_id . '"]';
+        $download_url = site_url('shared-file-download/' . $fileid);
+        $shortcode = '[qdr_tss_download id="' . $fileid . '"]';
         
         return new WP_REST_Response(array(
-            'media_id' => $media_id,
+            'fileid' => $fileid,
             'permalink' => $download_url,
             'shortcode' => $shortcode
         ), 201);