From 6014609a7888c084aeda90af2e603ee86b34629e Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 15 Aug 2014 10:21:36 +0200 Subject: [PATCH] Added support for filesizemin:X and filesizemax:X --- data/help.md | 2 ++ src/Helpers/TextHelper.php | 10 ++++++++-- src/Models/SearchParsers/PostSearchParser.php | 12 ++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/data/help.md b/data/help.md index 0f95292c..d1ce5997 100644 --- a/data/help.md +++ b/data/help.md @@ -38,6 +38,8 @@ Command | Description [search]date:2000-01-01[/search] | posted on January 1st, 2000 | - | [search]datemin:...[/search] | posted on `...` or later (format like in `date:`) | `date_min` | [search]datemax:...[/search] | posted on `...` or earlier (format like in `date:`) | `date_max` | +[search]filesizemin:7M[/search] | has size of at least 7 megabytes | `filesize_min` | +[search]filesizemax:30K[/search] | has size of at most 30 kilobytes | `filesize_max` | [search]id:1,2,3[/search] | having specific post ID | `ids` | [search]name:...[/search] | having specific post name (hash in full URLs) | `names`, `hash`, `hashes` | [search]idmin:5[/search] | posts with ID greater than or equal to @5 | `id_min` | diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index be32656c..c3b123b9 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -154,12 +154,18 @@ class TextHelper public static function stripBytesUnits($string) { - return self::stripUnits($string, 1024, ['B', 'K', 'M', 'G']); + $string = strtoupper($string); + if (!preg_match('/^\d+[KMG]?B?$/', $string)) + throw new SimpleException('Invalid format: ' . $string); + return self::stripUnits(strtoupper($string), 1024, ['B', 'K', 'M', 'G']); } public static function stripDecimalUnits($string) { - return self::stripUnits($string, 1000, ['', 'K', 'M']); + $string = strtoupper($string); + if (!preg_match('/^\d+[KM]?$/', $string)) + throw new SimpleException('Invalid format: ' . $string); + return self::stripUnits(strtoupper($string), 1000, ['', 'K', 'M']); } public static function parseMarkdown($text, $simple = false) diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index 309e5b6a..1f7efc74 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -162,6 +162,18 @@ class PostSearchParser extends AbstractSearchParser return Sql\Functors::equalsOrLesser('post.upload_date', new Sql\Binding($dateMax)); } + elseif (in_array($key, ['filesizemin', 'filesize_min'])) + { + $fileSizeMin = TextHelper::stripBytesUnits($value); + return Sql\Functors::equalsOrGreater('post.file_size', new Sql\Binding($fileSizeMin)); + } + + elseif (in_array($key, ['filesizemax', 'filesize_max'])) + { + $fileSizeMax = TextHelper::stripBytesUnits($value); + return Sql\Functors::equalsOrLesser('post.file_size', new Sql\Binding($fileSizeMax)); + } + elseif ($key == 'special') { $activeUser = Auth::getCurrentUser();