diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2024-03-02 21:33:34 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2024-03-02 21:33:34 +0000 |
commit | ac194d254a4b23e67cff58d4b9756fa7307a3748 (patch) | |
tree | 8a7fdeb4c5a6f74eaeda2aed53cac2771dbe6361 | |
parent | abaa158dd2801ab821ece81353ba480ca4f45d5e (diff) | |
parent | 8039e062c2428dfcf22325627a1ee9a9d48883cf (diff) |
Merge "Add getCacheKey static method to UploadFromUrl"
-rw-r--r-- | includes/upload/UploadFromUrl.php | 20 | ||||
-rw-r--r-- | tests/phpunit/includes/upload/UploadFromUrlTest.php | 11 |
2 files changed, 31 insertions, 0 deletions
diff --git a/includes/upload/UploadFromUrl.php b/includes/upload/UploadFromUrl.php index c5eaf8f820f3..8753913e331e 100644 --- a/includes/upload/UploadFromUrl.php +++ b/includes/upload/UploadFromUrl.php @@ -117,6 +117,26 @@ class UploadFromUrl extends UploadBase { } /** + * Provides a caching key for an upload from url set of parameters + * Used to set the status of an async job in UploadFromUrlJob + * and retreive it in frontend clients like ApiUpload. Will return the + * empty string if not all parameters are present. + * + * @param array $params + * @return string + */ + public static function getCacheKey( $params ) { + if ( !isset( $params['filename'] ) || !isset( $params['url'] ) ) { + return ""; + } else { + // We use sha1 here to ensure we have a fixed-length string of printable + // characters. There is no cryptography involved, so we just need a + // relatively fast function. + return sha1( sprintf( "%s|||%s", $params['filename'], $params['url'] ) ); + } + } + + /** * @return string[] */ private static function getAllowedHosts(): array { diff --git a/tests/phpunit/includes/upload/UploadFromUrlTest.php b/tests/phpunit/includes/upload/UploadFromUrlTest.php index da1aa7003c56..058963163027 100644 --- a/tests/phpunit/includes/upload/UploadFromUrlTest.php +++ b/tests/phpunit/includes/upload/UploadFromUrlTest.php @@ -292,4 +292,15 @@ class UploadFromUrlTest extends ApiTestCase { $this->assertUploadOk( $upload ); } + public function testUploadFromUrlCacheKey() { + // Test we get back a properly formatted sha1 key out + $key = UploadFromUrl::getCacheKey( [ 'filename' => 'test.png', 'url' => 'https://example.com/example.png' ] ); + $this->assertNotEmpty( $key ); + $this->assertMatchesRegularExpression( "/^[0-9a-f]{40}$/", $key ); + } + + public function testUploadFromUrlCacheKeyMissingParam() { + $this->assertSame( "", UploadFromUrl::getCacheKey( [] ) ); + } + } |