Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a file filter using a comma-separated list for volume cloning #1078

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/Http/Controllers/Api/Volumes/Filters/FilenameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function index($id, $pattern)
// Escape trailing backslashes, else there would be an error with ilike.
$pattern = preg_replace('/\\\\$/', '\\\\\\\\', $pattern);

$files = explode(',', $pattern);
if (count($files) > 1) {
$files = collect($files)->map(
function ($f) {
$filename = trim($f);
return strlen($filename) > 0 ? $filename : null;
}
)->whereNotNull();

return $files->chunk(1000)->flatMap(fn ($chunk)
=> $volume->files()->whereIn('filename', $chunk)->pluck('id'));
}

return $volume->files()
->where('filename', 'ilike', str_replace('*', '%', $pattern))
->pluck('id');
Expand Down
2 changes: 1 addition & 1 deletion resources/views/volumes/clone.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
v-on:keydown.enter="loadFilesMatchingPattern">
@endif
<span class="help-block">
Filter by using a pattern that matches specific file names. A pattern may contain the wildcard character * that matches any string of zero or more characters
Filter by using a list of comma-separated file names or pattern that matches specific file names. A pattern may contain the wildcard character * that matches any string of zero or more characters
</span>
<div class="volume-files-panel">
<ul class="list-group files-list">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,55 @@ public function testIndexVideo()
->assertSimilarJson([$video->id, $video2->id, $video3->id]);
$response->assertStatus(200);
}

public function testIndexCommaSeparatedImageFilenames()
{
$img = $this->volume()->id;

$image = ImageTest::create([
'volume_id' => $img,
'filename' => 'a.jpg',
]);
$image2 = ImageTest::create([
'volume_id' => $img,
'filename' => 'b.jpg',
]);

$this->beGuest();
$response = $this->json('GET', "/api/v1/volumes/{$img}/files/filter/filename/a.jpg")
->assertExactJson([$image->id]);
$response->assertStatus(200);

$response = $this->json('GET', "/api/v1/volumes/{$img}/files/filter/filename/a.jpg%2Cb.jpg")
->assertExactJson([$image->id, $image2->id]);
$response->assertStatus(200);

$response = $this->json('GET', "/api/v1/volumes/{$img}/files/filter/filename/%2C%2C%20%20a.jpg%2Cb.jpg%20")
->assertExactJson([$image->id, $image2->id]);
$response->assertStatus(200);
}

public function testIndexCommaSeparatedVideoFilenames()
{
$vid = $this->volume(['media_type_id' => MediaType::videoId()])->id;

$video = VideoTest::create([
'volume_id' => $vid,
'filename' => 'a.mp4',
]);
$video2 = VideoTest::create([
'volume_id' => $vid,
'filename' => 'b.mp4',
]);

$this->beGuest();

$response = $this->json('GET', "/api/v1/volumes/{$vid}/files/filter/filename/a.mp4%2Cb.mp4")
->assertExactJson([$video->id, $video2->id]);
$response->assertStatus(200);

$response = $this->json('GET', "/api/v1/volumes/{$vid}/files/filter/filename/%2C%2C%20%20a.mp4%2Cb.mp4%20")
->assertExactJson([$video->id, $video2->id]);
$response->assertStatus(200);
}
}