From 927489bc5b4bae5ef561d1bbb8f6ee27c68030a8 Mon Sep 17 00:00:00 2001 From: Darshan Date: Mon, 21 Apr 2025 12:04:36 +0530 Subject: [PATCH] update: handle decryption as well. --- app/controllers/api/migrations.php | 43 ++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/migrations.php b/app/controllers/api/migrations.php index a19c6d3a22..afb27b2c94 100644 --- a/app/controllers/api/migrations.php +++ b/app/controllers/api/migrations.php @@ -4,6 +4,7 @@ use Appwrite\Auth\Auth; use Appwrite\Event\Event; use Appwrite\Event\Migration; use Appwrite\Extend\Exception; +use Appwrite\OpenSSL\OpenSSL; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; @@ -31,6 +32,7 @@ use Utopia\Storage\Compression\Algorithms\GZIP; use Utopia\Storage\Compression\Algorithms\Zstd; use Utopia\Storage\Compression\Compression; use Utopia\Storage\Device; +use Utopia\System\System; use Utopia\Validator\ArrayList; use Utopia\Validator\Integer; use Utopia\Validator\Text; @@ -347,29 +349,42 @@ App::post('/v1/migrations/csv') throw new Exception(Exception::STORAGE_FILE_NOT_FOUND, 'File not found in ' . $path); } - if (!empty($file->getAttribute('openSSLCipher'))) { - throw new Exception(Exception::STORAGE_FILE_TYPE_UNSUPPORTED, "Only unencrypted CSV files can be used for document import."); - } - + // no encryption, compression on files above 20MB. + $hasEncryption = !empty($file->getAttribute('openSSLCipher')); $compression = $file->getAttribute('algorithm', Compression::NONE); - $hasCompression = $compression !== Compression::NONE; // skipped on files that are 20MB+ in size. + $hasCompression = $compression !== Compression::NONE; - // copy to import volume $migrationId = ID::unique(); $newPath = $deviceForImports->getPath('/' . $migrationId . '_' . $fileId . '.csv'); - if ($hasCompression) { + if ($hasEncryption || $hasCompression) { $source = $deviceForFiles->read($path); - switch ($compression) { - case Compression::ZSTD: - $source = (new Zstd())->decompress($source); - break; - case Compression::GZIP: - $source = (new GZIP())->decompress($source); - break; + // 1. decrypt + if ($hasEncryption) { + $source = OpenSSL::decrypt( + $source, + $file->getAttribute('openSSLCipher'), + System::getEnv('_APP_OPENSSL_KEY_V' . $file->getAttribute('openSSLVersion')), + 0, + hex2bin($file->getAttribute('openSSLIV')), + hex2bin($file->getAttribute('openSSLTag')) + ); } + // 2. decompress + if ($hasCompression) { + switch ($compression) { + case Compression::ZSTD: + $source = (new Zstd())->decompress($source); + break; + case Compression::GZIP: + $source = (new GZIP())->decompress($source); + break; + } + } + + // manual write after decryption and/or decompression if (! $deviceForImports->write($newPath, $source, 'text/csv')) { throw new \Exception("Unable to copy file"); }