class-qdr-tss-upload-controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Upload REST API Controller for the plugin.
  4. *
  5. * This class handles REST API endpoints for file upload operations.
  6. *
  7. * @package QDR_Temporary_Shared_Storage
  8. * @since 1.0.0
  9. */
  10. class QDR_TSS_Upload_Controller {
  11. /**
  12. * The plugin name.
  13. *
  14. * @since 1.0.0
  15. * @access private
  16. * @var string $plugin_name The name of this plugin.
  17. */
  18. private $plugin_name;
  19. /**
  20. * The plugin version.
  21. *
  22. * @since 1.0.0
  23. * @access private
  24. * @var string $version The version of this plugin.
  25. */
  26. private $version;
  27. /**
  28. * The namespace for the REST API routes.
  29. *
  30. * @since 1.0.0
  31. * @access private
  32. * @var string $namespace The namespace for the REST API routes.
  33. */
  34. private $namespace = 'qdr-tss/v1';
  35. /**
  36. * The database manager instance.
  37. *
  38. * @since 1.0.0
  39. * @access private
  40. * @var QDR_TSS_DB_Manager $db_manager The database manager instance.
  41. */
  42. private $db_manager;
  43. /**
  44. * The file manager instance.
  45. *
  46. * @since 1.0.0
  47. * @access private
  48. * @var QDR_TSS_File_Manager $file_manager The file manager instance.
  49. */
  50. private $file_manager;
  51. /**
  52. * The settings instance.
  53. *
  54. * @since 1.0.0
  55. * @access private
  56. * @var QDR_TSS_Settings $settings The settings instance.
  57. */
  58. private $settings;
  59. /**
  60. * Initialize the class and set its properties.
  61. *
  62. * @since 1.0.0
  63. * @param string $plugin_name The name of this plugin.
  64. * @param string $version The version of this plugin.
  65. */
  66. public function __construct($plugin_name, $version) {
  67. $this->plugin_name = $plugin_name;
  68. $this->version = $version;
  69. $this->db_manager = QDR_TSS_DB_Manager::instance();
  70. $this->file_manager = QDR_TSS_File_Manager::instance();
  71. $this->settings = QDR_TSS_Settings::instance();
  72. }
  73. /**
  74. * Register the REST API routes.
  75. *
  76. * @since 1.0.0
  77. */
  78. public function register_routes() {
  79. // Chunked upload endpoints
  80. register_rest_route($this->namespace, '/upload/init', array(
  81. 'methods' => WP_REST_Server::CREATABLE,
  82. 'callback' => array($this, 'init_chunked_upload'),
  83. 'permission_callback' => array($this, 'check_permission'),
  84. ));
  85. register_rest_route($this->namespace, '/upload/chunk', array(
  86. 'methods' => WP_REST_Server::CREATABLE,
  87. 'callback' => array($this, 'upload_chunk'),
  88. 'permission_callback' => array($this, 'check_permission'),
  89. ));
  90. register_rest_route($this->namespace, '/upload/finalize', array(
  91. 'methods' => WP_REST_Server::CREATABLE,
  92. 'callback' => array($this, 'finalize_chunked_upload'),
  93. 'permission_callback' => array($this, 'check_permission'),
  94. ));
  95. }
  96. /**
  97. * Check permission for REST API requests.
  98. *
  99. * @since 1.0.0
  100. * @return bool True if authorized, false otherwise.
  101. */
  102. public function check_permission() {
  103. $rest_controller = new QDR_TSS_REST_Controller($this->plugin_name, $this->version);
  104. return $rest_controller->check_rest_permission();
  105. }
  106. /**
  107. * Initialize chunked upload.
  108. *
  109. * @since 1.0.0
  110. * @param WP_REST_Request $request Full details about the request.
  111. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  112. */
  113. public function init_chunked_upload($request) {
  114. // Check required parameters
  115. if (!isset($request['original_file_name']) || !isset($request['file_size'])) {
  116. return new WP_Error(
  117. 'qdr_tss_missing_params',
  118. __('Missing required parameters.', 'qdr-temporary-shared-storage'),
  119. array('status' => 400)
  120. );
  121. }
  122. $file_size = (int) $request['file_size'];
  123. $max_upload_size = $this->settings->get_setting('max_upload_size');
  124. // Check max upload size
  125. if ($file_size > $max_upload_size) {
  126. return new WP_Error(
  127. 'qdr_tss_file_too_large',
  128. __('File exceeds maximum upload size.', 'qdr-temporary-shared-storage'),
  129. array('status' => 400)
  130. );
  131. }
  132. // Check total storage usage
  133. if ($this->db_manager->would_exceed_storage_limit($file_size, $max_upload_size)) {
  134. return new WP_Error(
  135. 'qdr_tss_storage_limit',
  136. __('Storage limit exceeded.', 'qdr-temporary-shared-storage'),
  137. array('status' => 400)
  138. );
  139. }
  140. // Initialize chunked upload
  141. $upload_id = $this->file_manager->init_chunked_upload();
  142. if (!$upload_id) {
  143. return new WP_Error(
  144. 'qdr_tss_init_failed',
  145. __('Failed to initialize upload.', 'qdr-temporary-shared-storage'),
  146. array('status' => 500)
  147. );
  148. }
  149. return new WP_REST_Response(array(
  150. 'upload_id' => $upload_id,
  151. 'status' => 'initialized'
  152. ), 200);
  153. }
  154. /**
  155. * Upload a chunk.
  156. *
  157. * @since 1.0.0
  158. * @param WP_REST_Request $request Full details about the request.
  159. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  160. */
  161. public function upload_chunk($request) {
  162. // Check required parameters
  163. if (!isset($request['upload_id']) || !isset($request['chunk_index']) || !isset($request['total_chunks'])) {
  164. return new WP_Error(
  165. 'qdr_tss_missing_params',
  166. __('Missing required parameters.', 'qdr-temporary-shared-storage'),
  167. array('status' => 400)
  168. );
  169. }
  170. $upload_id = $request['upload_id'];
  171. $chunk_index = (int) $request['chunk_index'];
  172. $total_chunks = (int) $request['total_chunks'];
  173. // Handle file upload
  174. $files = $request->get_file_params();
  175. if (empty($files) || !isset($files['chunk'])) {
  176. return new WP_Error(
  177. 'qdr_tss_no_file',
  178. __('No file uploaded.', 'qdr-temporary-shared-storage'),
  179. array('status' => 400)
  180. );
  181. }
  182. $chunk_file = $files['chunk'];
  183. // Store chunk
  184. $result = $this->file_manager->store_chunk($upload_id, $chunk_index, $chunk_file);
  185. if (!$result) {
  186. return new WP_Error(
  187. 'qdr_tss_chunk_failed',
  188. __('Failed to upload chunk.', 'qdr-temporary-shared-storage'),
  189. array('status' => 500)
  190. );
  191. }
  192. return new WP_REST_Response(array(
  193. 'upload_id' => $upload_id,
  194. 'chunk_index' => $chunk_index,
  195. 'status' => 'chunk_uploaded'
  196. ), 200);
  197. }
  198. /**
  199. * Finalize chunked upload.
  200. *
  201. * @since 1.0.0
  202. * @param WP_REST_Request $request Full details about the request.
  203. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  204. */
  205. public function finalize_chunked_upload($request) {
  206. // Check required parameters
  207. if (!isset($request['upload_id']) || !isset($request['total_chunks'])) {
  208. return new WP_Error(
  209. 'qdr_tss_missing_params',
  210. __('Missing required parameters.', 'qdr-temporary-shared-storage'),
  211. array('status' => 400)
  212. );
  213. }
  214. $upload_id = $request['upload_id'];
  215. $total_chunks = (int) $request['total_chunks'];
  216. // Check for required file metadata
  217. $required_fields = array('original_file_name', 'description', 'password');
  218. foreach ($required_fields as $field) {
  219. if (!isset($request[$field]) || empty($request[$field])) {
  220. return new WP_Error(
  221. 'qdr_tss_missing_field',
  222. sprintf(__('Missing required field: %s', 'qdr-temporary-shared-storage'), $field),
  223. array('status' => 400)
  224. );
  225. }
  226. }
  227. // Check that all chunks are present
  228. if (!$this->file_manager->check_all_chunks($upload_id, $total_chunks)) {
  229. return new WP_Error(
  230. 'qdr_tss_missing_chunks',
  231. __('Not all chunks have been uploaded.', 'qdr-temporary-shared-storage'),
  232. array('status' => 400)
  233. );
  234. }
  235. // Finalize upload
  236. $result = $this->file_manager->finalize_chunked_upload(
  237. $upload_id,
  238. $total_chunks,
  239. $request['original_file_name']
  240. );
  241. if (!$result) {
  242. return new WP_Error(
  243. 'qdr_tss_finalize_failed',
  244. __('Failed to finalize upload.', 'qdr-temporary-shared-storage'),
  245. array('status' => 500)
  246. );
  247. }
  248. $file_path = $result['file_path'];
  249. $file_size = $result['file_size'];
  250. // Set dates
  251. $active_from = !empty($request['active_from']) ? $request['active_from'] : current_time('mysql');
  252. $active_to = !empty($request['active_to']) ? $request['active_to'] : date('Y-m-d H:i:s', strtotime('+30 days'));
  253. // Generate a unique media ID
  254. $media_id = time() . rand(1000, 9999);
  255. // Insert into database
  256. $data = array(
  257. 'media_id' => $media_id,
  258. 'original_file_name' => sanitize_file_name($request['original_file_name']),
  259. 'description' => sanitize_textarea_field($request['description']),
  260. 'message' => isset($request['message']) ? sanitize_textarea_field($request['message']) : '',
  261. 'active_from' => $active_from,
  262. 'active_to' => $active_to,
  263. 'password' => wp_hash_password($request['password']),
  264. 'reference' => isset($request['reference']) ? sanitize_text_field($request['reference']) : '',
  265. 'upload_date' => current_time('mysql'),
  266. 'file_path' => $file_path,
  267. 'file_size' => $file_size
  268. );
  269. $item_id = $this->db_manager->insert_item($data);
  270. if (!$item_id) {
  271. // Delete file if database insert fails
  272. $this->file_manager->delete_file($file_path);
  273. return new WP_Error(
  274. 'qdr_tss_db_failed',
  275. __('Failed to save file information.', 'qdr-temporary-shared-storage'),
  276. array('status' => 500)
  277. );
  278. }
  279. $download_url = site_url('shared-file-download/' . $media_id);
  280. $shortcode = '[qdr_tss_download id="' . $media_id . '"]';
  281. return new WP_REST_Response(array(
  282. 'media_id' => $media_id,
  283. 'permalink' => $download_url,
  284. 'shortcode' => $shortcode
  285. ), 201);
  286. }
  287. }