Skip to content

Commit

Permalink
Only use /uploads/nostr/a/b path for nip96 uploads, not other WordPre…
Browse files Browse the repository at this point in the history
…ss uploads
  • Loading branch information
fabianfabian committed Oct 26, 2023
1 parent d533f78 commit 54a2c7b
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions nostr-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,31 @@ function my_plugin_admin_notices() {
// We store in /a/b/hash.ext where /a/b/ is first 2 letters of the hash, so folder browsing does not become slow with too many files in one folder.

function custom_upload_dir($uploads) {
// Assuming $original_hash is accessible here (otherwise, you'll need to calculate it again)
global $original_hash; // We'll set this in the file handling code later

$base_directory = WP_CONTENT_DIR . '/uploads/nostr';
$base_url = content_url('/uploads/nostr');

$custom_directory = '/' . substr($original_hash, 0, 1) . '/' . substr($original_hash, 1, 1);
$custom_url = '/' . substr($original_hash, 0, 1) . '/' . substr($original_hash, 1, 1);

$uploads['path'] = $base_directory . $custom_directory;
if (!file_exists($uploads['path'])) {
wp_mkdir_p($uploads['path']);
}
$uploads['url'] = $base_url . $custom_url;

$uploads['subdir'] = $custom_directory;
$uploads['basedir'] = $base_directory;
$uploads['baseurl'] = $base_url;
// Check if we are in the specific REST route
$current_route = $_SERVER['REQUEST_URI'] ?? '';

if (strpos($current_route, 'nostrmedia/v1/upload') !== false) {

// Assuming $original_hash is accessible here (otherwise, you'll need to calculate it again)
global $original_hash; // We'll set this in the file handling code later

$base_directory = WP_CONTENT_DIR . '/uploads/nostr';
$base_url = content_url('/uploads/nostr');

$custom_directory = '/' . substr($original_hash, 0, 1) . '/' . substr($original_hash, 1, 1);
$custom_url = '/' . substr($original_hash, 0, 1) . '/' . substr($original_hash, 1, 1);

$uploads['path'] = $base_directory . $custom_directory;
if (!file_exists($uploads['path'])) {
wp_mkdir_p($uploads['path']);
}
$uploads['url'] = $base_url . $custom_url;

$uploads['subdir'] = $custom_directory;
$uploads['basedir'] = $base_directory;
$uploads['baseurl'] = $base_url;
return $uploads;
}

return $uploads;
}
Expand Down

0 comments on commit 54a2c7b

Please sign in to comment.