Skip to content

Commit

Permalink
Merge pull request #8 from DeepakBairagi-Fragmatic/master
Browse files Browse the repository at this point in the history
master: optimized space complexity of the get media function to avoid…
  • Loading branch information
Saadmairaj authored Jan 31, 2024
2 parents 7bb8449 + de9e948 commit 7636ec2
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions api/endpoints/get-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ function get_media($request)
$raw_page = $request->get_param('page');
$media_id = $request->get_param('id');
$search_name = $request->get_param('name');
$batch_size = 50; // Adjust the batch size based on your server's memory limit
$batch_size = 500;

if ($media_id !== null) {
// Process a single media item
$single_media = get_post($media_id);

if (!$single_media || $single_media->post_type !== 'attachment') {
Expand All @@ -54,44 +53,34 @@ function get_media($request)

$query_args = [
'post_type' => 'attachment',
'posts_per_page' => $batch_size,
'post_status' => 'inherit',
'orderby' => 'date',
'order' => 'DESC',
'post_mime_type' => array('image/jpeg', 'image/jpg', 'image/png'),
's' => $search_name
];

if (!empty($search_name)) {
$query_args['s'] = $search_name;
}

$all_media = [];
$paged = 1;
$allowed_extensions = ['jpg', 'jpeg', 'png', 'JPG', 'JPEG'];
$total_items = 0;
$total_pages = ceil($total_items / $per_page) - 1;

// Process batches of media until all items are fetched
do {
$query_args['paged'] = $paged;
$query_args['posts_per_page'] = $batch_size,
$query_args['paged'] = $paged,
$batch_media = get_posts($query_args);

foreach ($batch_media as $item) {
$file_extension = pathinfo(get_attached_file($item->ID), PATHINFO_EXTENSION);

if (in_array(strtolower($file_extension), $allowed_extensions)) {
$all_media[] = $item;
}
}

$total_items = $total_items + count($batch_media)
$paged++;

} while (!empty($batch_media));

$total_items = count($all_media);
$total_pages = ceil($total_items / $per_page) - 1;

if ($page > $total_pages && $total_items > 0) {
return new WP_Error('invalid_page', 'Invalid page, please check!', ['status' => 400]);
}

$start_index = $page * $per_page;
$paginated_media = array_slice($all_media, $start_index, $per_page);
$query_args['posts_per_page'] = $per_page,
$query_args['paged'] = $page + 1,
$paginated_media = get_posts($query_args)

$response = [
'results' => array_map('get_media_response_data', $paginated_media),
Expand All @@ -113,11 +102,7 @@ function get_media_response_data($media)
'mid' => $media->ID,
'link' => wp_get_attachment_url($media->ID),
'name' => $media->post_name,
'status' => $media->post_status,
'mime_type' => get_post_mime_type($media->ID),
'file_format' => pathinfo(get_attached_file($media->ID), PATHINFO_EXTENSION),
'alt_text' => get_post_meta($media->ID, '_wp_attachment_image_alt', true),
'caption' => $media->post_excerpt,
'created' => $media->post_date_gmt,
];
}

0 comments on commit 7636ec2

Please sign in to comment.