Add optional setting to use system temp directory for H5P temporary files - #204
Add optional setting to use system temp directory for H5P temporary files#204Lin87 wants to merge 6 commits into
Conversation
Introduces a new admin setting allowing users to store temporary files in the server's system temp folder instead of the plugin directory. Updates settings UI, option handling, and file path logic to support this feature, improving performance on resource-limited servers.
otacke
left a comment
There was a problem hiding this comment.
@Lin87 Hi Ethan! As you may have heard, I am now a community maintainer, and I started to browse some of the issues and pull-requests across H5P Group's repositories, so they will eventually be handled.
I think your pull request makes sense in general! I cannot decide whether it will be accepted or rejected, however. Nevertheless did a review and noted some findings that you might want to check, see comments.
Unfortunately, you did not pre-qualify the change, because I think optionally writing to the server's /tmp directory instead would not only be a smart idea to have for WordPress, but for any H5P integration. Then getTmpPath in H5P core (https://github.com/h5p/h5p-php-library/blob/5e6058c88b5641bdda9167f25cbee4881cf7b03b/h5p-default-storage.class.php#L103-L107) would need to get the switch, and H5P itegrations would pass that either in the function call or via an environment variable (that might be set on the server even without an option in the settings).
I'll /cc @icc here to comment on whether optionally using /tmp would be considered by H5P Group and whether it should be implemented in H5P core or per H5P integration.
Cheers,
Oliver
p.s.: You should better not develop on your master branch and create pull requests from it. It prevents you from properly syncing changes, and if your pull request gets rejected, you'd have to clean up the repository to bring it to a valid state again. Can be done, sure, but it's probably better to prevent something like that by simply working on a dedicated branch.
|
@Lin87 Do you plan to take care of the comments I added? |
|
@otacke So sorry. Work has been hectic. I will set some time aside next week and tackle this. |
|
@Lin87 No worries. I am not trying to push. But I closed other pull requests today, because the original contributor was not interested in doing the changes requested. That's why I asked. |
| $plugin = H5P_Plugin::get_instance(); | ||
| $core = $plugin->get_h5p_instance('core'); | ||
| $dir = $core->fs->getTmpPath(); | ||
| if (get_option('h5p_use_system_temp_dir', FALSE)) { |
There was a problem hiding this comment.
As I said, having the option to have this in other H5P integrations, too, would be benefitial. I am pinging @icc here (currently on vacation AFAIK) as I cannot tell whether that's in the interest of H5P Group.
Then getTmpPath in H5P core should return the path and either get an argument to switch between integration/temp or learn about the setting in a different fashion.
Add helper methods to manage temporary directory paths with improved security. System temp staging now uses a dedicated, non-listable subfolder with restricted permissions (0700) instead of directly in the system temp directory, preventing local users from predicting paths. This refactoring also reduces code duplication in getUploadedH5PFolderPath() and getTmpPath() methods while preserving fallback to plugin temp directory if system temp is unavailable.
Add deletion of the h5p_use_system_temp_dir option to the cleanup routine, ensuring this setting is properly removed during plugin deactivation.
Option to use system temp dir
Extract the H5P staging directory path logic into a reusable static method (getSystemTmpParentPath), enable cleanup of the system temp staging folder when empty, and improve temporary folder naming by using wp_generate_password for better uniqueness instead of uniqid.
|
@otacke Thank you for reviewing my pull request and sorry for the delayed response. Great catches on my mistakes. I skipped a couple of steps when I copied the code from my colleague's pull request. Good thing the code is correct on my live websites. Anyway, I made a few changes to the code. I rewrote the
The When I took over this task some time ago, I considered proposing that these changes be made in the H5P PHP core rather than in the WordPress integration. I decided against that approach for three reasons:
That said, if the H5P Group believes using the system temp directory across all integrations would be beneficial, I would fully support that approach. For now, however, I need this functionality to remain in the WordPress integration for the websites I currently manage at my organization. We host our WordPress sites on AWS, and user-uploaded files, including plugins, themes, and media uploads, are stored in an Amazon S3 bucket. That setup is slower and not well suited for temporary files, so using the system temp directory is important for our environment for the following benefits:
P.S. I understand that I should not use the Regardless of whether this pull request is approved, I need to use my modified version of the plugin so that it works better in my environment. Having this option available natively in the official plugin would make things much easier, since I would not need to port or resync the changes every time a new version is released. Please let me know if I need to make any further changes or if you have questions. Thanks again for reviewing my changes. Ethan |
Both. I coded what I wanted it to do first and then have AI code review my changes to make sure my code does not break anything else, and the format remain consistent with the rest of the plugin code. Sometimes, AI suggests better coding logic than mine, which I reviewed and agreed to the change. |
|
@Lin87 As mentioned in my initial comment, Here's how this could look: This way, nothing would change for other H5P integrations when calling
Long story short: I am not the final reviewer anyway, so @icc or someone else from H5P Group should comment further. Oh, and my question about the use of AI was triggered by the overly verbose commenting, a common footprint. |
|
@otacke Thank you for the additional information and code sample. Is the H5P core repository open to public contributions? I noticed that it has its own public GitHub repository, but I am not sure whether pull requests there receive the same attention as contributions to the WordPress and other platform integrations. As you said, I will wait for someone from the H5P Group to comment on this request and provide guidance on the next steps. I am fine with either approach as long as the option to use the system temp directory is available. I intentionally forced AI to generate/keep verbose comments. They help me verify that the code does what it said it does. Thank you again for reviewing and verifying this pull request. |
|
@Lin87 All public H5P repositories are open for contributions. Regarding code verbosity, if you need it for understanding the code, fine. But you can in fact comment too much. If you need comments to understand what the code is doing, then the code might not be up to highest standards. I can recommend to take a look at the book "Clean Code" by Robert C. Martin (or at least https://blog.cleancoder.com/uncle-bob/2017/02/23/NecessaryComments.html). I'd request to boil down comments, but I'll leave that to H5P Group. |
Hello,
This pull request introduces an optional admin setting that allows H5P to store temporary/working files in the server’s system temporary directory (via PHP's sys_get_temp_dir() function) instead of the H5P plugin directory.
Background
In our environment, WordPress content is stored on AWS Elastic File System (EFS). While EFS works well for persistent content, it performs poorly when handling large numbers of small, frequently changing files—such as those created during H5P uploads and processing.
By using the server's system temp directory for these temporary files, we've observed:
This aligns with common best practices of using local system temp storage for short-lived working files.
What This PR Does
Backward Compatibility & Safety
This pull request is based on the earlier work from PR #130, originally submitted several years ago. That PR was created by my colleague and did not receive any follow-up, so this submission reintroduces the feature with minor updates to align with the current H5P codebase.
Thank you for considering this improvement. I'm happy to adjust the implementation or documentation if needed.