This commit is contained in:
Sanskar Tiwari 2024-03-08 08:48:11 +05:30
parent a09ed132b8
commit 6748862abd
6 changed files with 395 additions and 113 deletions

View file

@ -15,44 +15,49 @@ class BundleController extends Controller
{ {
// The bundle content preview // The bundle content preview
public function previewBundle(Request $request, Bundle $bundle) { public function previewBundle(Request $request, Bundle $bundle)
{
return view('download', [ return view('download', [
'bundle' => new BundleResource($bundle) 'bundle' => new BundleResource($bundle)
]); ]);
} }
// The download method // The download method
// - the bundle // - the bundle
// - or just one file // - or just one file
public function downloadZip(Request $request, Bundle $bundle) { public function downloadZip(Request $request, Bundle $bundle)
{
try { // try {
// Download of the full bundle // Download of the full bundle
// We must create a Zip archive // We must create a Zip archive
$bundle->downloads ++; $bundle->downloads++;
$bundle->save(); $bundle->save();
$filename = Storage::disk('uploads')->path('').'/'.$bundle->slug.'/bundle.zip'; $filename = $bundle->slug . '/bundle.zip';
if (! file_exists($filename)) { if (!Storage::exists($filename)) {
$bundlezip = fopen($filename, 'w'); // creating the zip archive
// Creating the archive
$zip = new ZipArchive; $zip = new ZipArchive;
if (! @$zip->open($filename, ZipArchive::CREATE)) { $tempFilePath = tempnam(sys_get_temp_dir(), 'zip');
if (!$zip->open($tempFilePath, ZipArchive::CREATE)) {
throw new Exception('Cannot initialize Zip archive'); throw new Exception('Cannot initialize Zip archive');
} }
// Setting password when required // Setting password when required
if (! empty($bundle->password)) { if (!empty($bundle->password)) {
$zip->setPassword($bundle->password); $zip->setPassword($bundle->password);
} }
// Adding the files into the Zip with their real names // Adding the files into the Zip with their real names
foreach ($bundle->files as $k => $file) { foreach ($bundle->files as $k => $file) {
if (file_exists(config('filesystems.disks.uploads.root').'/'.$file->fullpath)) { // get the bundle/filename
// dd((Storage::getConfig()['root'] ?? '') . '/' . $file->fullpath);
if (Storage::exists($file->fullpath)) {
$name = $file->original; $name = $file->original;
$stream = Storage::get($file->fullpath);
// If a file in the archive has the same name // If a file in the archive has the same name
if (false !== $zip->locateName($name)) { if (false !== $zip->locateName($name)) {
@ -65,63 +70,64 @@ class BundleController extends Controller
// Looping to find the right name // Looping to find the right name
do { do {
$i++; $i++;
$newname = $basename.'-'.$i.$extension; $newname = $basename . '-' . $i . $extension;
} } while (false !== $zip->locateName($newname));
while ($zip->locateName($newname));
// Final name was found // Final name was found
$name = $newname; $name = $newname;
dd(2);
} }
// Finally adding files // Finally adding files
$zip->addFile(config('filesystems.disks.uploads.root').'/'.$file->fullpath, $name); $zip->addFromString($name, $stream);
if (! empty($bundle->password)) { if (!empty($bundle->password)) {
$zip->setEncryptionIndex($k, ZipArchive::EM_AES_256); $zip->setEncryptionIndex($k, ZipArchive::EM_AES_256);
} }
} }
} }
if (! @$zip->close()) {
if (!@$zip->close()) {
throw new Exception('Cannot close Zip archive'); throw new Exception('Cannot close Zip archive');
} }
Storage::put($filename, file_get_contents($tempFilePath));
fclose($bundlezip);
} }
if (Storage::getConfig()['driver'] == 'local') {
// Getting file size // Getting file size
$filesize = filesize($filename); $filesize = filesize(Storage::path($filename));
// Let's download now // Let's download now
header('Content-Type: application/octet-stream'); return response()->streamDownload(function () use ($filename) {
header('Content-Disposition: attachment; filename="'.Str::slug($bundle->title).'-'.time().'.zip'.'"');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Content-Length: '.$filesize);
// Downloading // Downloading
if (config('sharing.download_limit_rate', false) !== false) { if (config('sharing.download_limit_rate', false) !== false) {
$limit_rate = Upload::humanReadableToBytes(config('sharing.download_limit_rate')); $limit_rate = Upload::humanReadableToBytes(config('sharing.download_limit_rate'));
$fh = fopen($filename, 'rb'); $fh = fopen(Storage::path($filename), 'rb');
while (! feof($fh)) { while (!feof($fh)) {
echo fread($fh, round($limit_rate)); echo fread($fh, round($limit_rate));
flush(); flush();
sleep(1); sleep(1);
} }
fclose($filename); fclose($filename);
} else {
readfile(Storage::path($filename));
} }
else { }, Str::slug($bundle->title) . '-' . time() . '.zip', [
readfile($filename); 'Content-Length' => $filesize,
'Content-Type' => 'application/zip'
]);
} else if (Storage::getConfig()['driver'] == 's3') {
// Cannot limit the download rate
return Storage::download($filename, Str::slug($bundle->title) . '-' . time() . '.zip');
} else {
// Handle other drivers
} }
exit;
} }
// Could not find the metadata file // Could not find the metadata file
catch (Exception $e) { // catch (Exception $e) {
abort(500, $e->getMessage()); // abort(500, $e->getMessage());
} // }
// }
}
} }

View file

@ -16,7 +16,8 @@ use App\Models\File;
class UploadController extends Controller class UploadController extends Controller
{ {
public function createBundle(Request $request, Bundle $bundle) { public function createBundle(Request $request, Bundle $bundle)
{
return view('upload', [ return view('upload', [
'bundle' => new BundleResource($bundle), 'bundle' => new BundleResource($bundle),
'baseUrl' => config('app.url') 'baseUrl' => config('app.url')
@ -24,7 +25,8 @@ class UploadController extends Controller
} }
// The upload form // The upload form
public function storeBundle(Request $request, Bundle $bundle) { public function storeBundle(Request $request, Bundle $bundle)
{
try { try {
$bundle->update([ $bundle->update([
@ -36,8 +38,7 @@ class UploadController extends Controller
]); ]);
return response()->json(new BundleResource($bundle)); return response()->json(new BundleResource($bundle));
} } catch (Exception $e) {
catch (Exception $e) {
return response()->json([ return response()->json([
'result' => false, 'result' => false,
'message' => $e->getMessage() 'message' => $e->getMessage()
@ -45,17 +46,18 @@ class UploadController extends Controller
} }
} }
public function uploadFile(Request $request, Bundle $bundle) { public function uploadFile(Request $request, Bundle $bundle)
{
// Validating form data // Validating form data
$request->validate([ $request->validate([
'uuid' => 'required|uuid', 'uuid' => 'required|uuid',
'file' => 'required|file|max:'.(Upload::fileMaxSize() / 1000) 'file' => 'required|file|max:' . (Upload::fileMaxSize() / 1000)
]); ]);
// Generating the file name // Generating the file name
$original = $request->file->getClientOriginalName(); $original = $request->file->getClientOriginalName();
$filename = substr(sha1($original.time()), 0, rand(20, 30)); $filename = substr(sha1($original . time()), 0, rand(20, 30));
// Moving file to final destination // Moving file to final destination
try { try {
@ -64,13 +66,14 @@ class UploadController extends Controller
$hash = sha1_file($request->file->getPathname()); $hash = sha1_file($request->file->getPathname());
$existing = $bundle->files->whereNotNull('hash')->where('hash', $hash)->count(); $existing = $bundle->files->whereNotNull('hash')->where('hash', $hash)->count();
if (! empty($existing) && $existing > 0) { if (!empty($existing) && $existing > 0) {
throw new Exception(__('app.duplicate-file')); throw new Exception(__('app.duplicate-file'));
} }
} }
$fullpath = $request->file('file')->storeAs( $fullpath = $request->file('file')->storeAs(
$bundle->slug, $filename, 'uploads' $bundle->slug,
$filename
); );
// Generating file metadata // Generating file metadata
$file = new File([ $file = new File([
@ -87,8 +90,7 @@ class UploadController extends Controller
$file->save(); $file->save();
return response()->json(new FileResource($file)); return response()->json(new FileResource($file));
} } catch (Exception $e) {
catch (Exception $e) {
return response()->json([ return response()->json([
'result' => false, 'result' => false,
'message' => $e->getMessage() 'message' => $e->getMessage()
@ -96,7 +98,8 @@ class UploadController extends Controller
} }
} }
public function deleteFile(Request $request, Bundle $bundle) { public function deleteFile(Request $request, Bundle $bundle)
{
$request->validate([ $request->validate([
'uuid' => 'required|uuid' 'uuid' => 'required|uuid'
@ -107,7 +110,7 @@ class UploadController extends Controller
$file = File::findOrFail($request->uuid); $file = File::findOrFail($request->uuid);
// Physically deleting the file // Physically deleting the file
if (! Storage::disk('uploads')->delete($file->fullpath)) { if (!Storage::delete($file->fullpath)) {
throw new Exception('Cannot delete file from disk'); throw new Exception('Cannot delete file from disk');
} }
@ -115,8 +118,7 @@ class UploadController extends Controller
$file->delete(); $file->delete();
return response()->json(new BundleResource($bundle)); return response()->json(new BundleResource($bundle));
} } catch (Exception $e) {
catch (Exception $e) {
return response()->json([ return response()->json([
'result' => false, 'result' => false,
'message' => $e->getMessage() 'message' => $e->getMessage()
@ -125,7 +127,8 @@ class UploadController extends Controller
} }
public function completeBundle(Request $request, Bundle $bundle) { public function completeBundle(Request $request, Bundle $bundle)
{
// Processing size // Processing size
$size = 0; $size = 0;
@ -140,9 +143,8 @@ class UploadController extends Controller
// Infinite expiry // Infinite expiry
if ($bundle->expiry == 'forever') { if ($bundle->expiry == 'forever') {
$bundle->expires_at = null; $bundle->expires_at = null;
} } else {
else { $bundle->expires_at = time() + $bundle->expiry;
$bundle->expires_at = time()+$bundle->expiry;
} }
$bundle->fullsize = $size; $bundle->fullsize = $size;
$bundle->preview_link = route('bundle.preview', ['bundle' => $bundle, 'auth' => $bundle->preview_token]); $bundle->preview_link = route('bundle.preview', ['bundle' => $bundle, 'auth' => $bundle->preview_token]);
@ -151,8 +153,7 @@ class UploadController extends Controller
$bundle->save(); $bundle->save();
return response()->json(new BundleResource($bundle)); return response()->json(new BundleResource($bundle));
} } catch (\Exception $e) {
catch (\Exception $e) {
return response()->json([ return response()->json([
'result' => false, 'result' => false,
'message' => $e->getMessage() 'message' => $e->getMessage()
@ -166,7 +167,8 @@ class UploadController extends Controller
* We invalidate the expiry date and let the CRON * We invalidate the expiry date and let the CRON
* task do the hard work * task do the hard work
*/ */
public function deleteBundle(Request $request, Bundle $bundle) { public function deleteBundle(Request $request, Bundle $bundle)
{
try { try {
// Forcing bundle to expire // Forcing bundle to expire
@ -184,13 +186,11 @@ class UploadController extends Controller
return response()->json([ return response()->json([
'success' => true 'success' => true
]); ]);
} } catch (Exception $e) {
catch (Exception $e) {
return response()->json([ return response()->json([
'success' => false, 'success' => false,
'message' => $e->getMessage() 'message' => $e->getMessage()
], 500); ], 500);
} }
} }
} }

View file

@ -11,6 +11,7 @@
"laravel/framework": "^10.8", "laravel/framework": "^10.8",
"laravel/sanctum": "^3.2", "laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8", "laravel/tinker": "^2.8",
"league/flysystem-aws-s3-v3": "^3.24",
"ryangjchandler/orbit": "^1.2" "ryangjchandler/orbit": "^1.2"
}, },
"require-dev": { "require-dev": {

282
composer.lock generated
View file

@ -4,8 +4,157 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "eb9f74c42bee8efdc1afdfe706ba40c3", "content-hash": "f498eb116b2db824f3d207fb38abfb63",
"packages": [ "packages": [
{
"name": "aws/aws-crt-php",
"version": "v1.2.4",
"source": {
"type": "git",
"url": "https://github.com/awslabs/aws-crt-php.git",
"reference": "eb0c6e4e142224a10b08f49ebf87f32611d162b2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/awslabs/aws-crt-php/zipball/eb0c6e4e142224a10b08f49ebf87f32611d162b2",
"reference": "eb0c6e4e142224a10b08f49ebf87f32611d162b2",
"shasum": ""
},
"require": {
"php": ">=5.5"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35||^5.6.3||^9.5",
"yoast/phpunit-polyfills": "^1.0"
},
"suggest": {
"ext-awscrt": "Make sure you install awscrt native extension to use any of the functionality."
},
"type": "library",
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "AWS SDK Common Runtime Team",
"email": "aws-sdk-common-runtime@amazon.com"
}
],
"description": "AWS Common Runtime for PHP",
"homepage": "https://github.com/awslabs/aws-crt-php",
"keywords": [
"amazon",
"aws",
"crt",
"sdk"
],
"support": {
"issues": "https://github.com/awslabs/aws-crt-php/issues",
"source": "https://github.com/awslabs/aws-crt-php/tree/v1.2.4"
},
"time": "2023-11-08T00:42:13+00:00"
},
{
"name": "aws/aws-sdk-php",
"version": "3.300.12",
"source": {
"type": "git",
"url": "https://github.com/aws/aws-sdk-php.git",
"reference": "0c38aec51d8dc76d6b56a6be05c37b0aff86de2d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/0c38aec51d8dc76d6b56a6be05c37b0aff86de2d",
"reference": "0c38aec51d8dc76d6b56a6be05c37b0aff86de2d",
"shasum": ""
},
"require": {
"aws/aws-crt-php": "^1.2.3",
"ext-json": "*",
"ext-pcre": "*",
"ext-simplexml": "*",
"guzzlehttp/guzzle": "^6.5.8 || ^7.4.5",
"guzzlehttp/promises": "^1.4.0 || ^2.0",
"guzzlehttp/psr7": "^1.9.1 || ^2.4.5",
"mtdowling/jmespath.php": "^2.6",
"php": ">=7.2.5",
"psr/http-message": "^1.0 || ^2.0"
},
"require-dev": {
"andrewsville/php-token-reflection": "^1.4",
"aws/aws-php-sns-message-validator": "~1.0",
"behat/behat": "~3.0",
"composer/composer": "^1.10.22",
"dms/phpunit-arraysubset-asserts": "^0.4.0",
"doctrine/cache": "~1.4",
"ext-dom": "*",
"ext-openssl": "*",
"ext-pcntl": "*",
"ext-sockets": "*",
"nette/neon": "^2.3",
"paragonie/random_compat": ">= 2",
"phpunit/phpunit": "^5.6.3 || ^8.5 || ^9.5",
"psr/cache": "^1.0",
"psr/simple-cache": "^1.0",
"sebastian/comparator": "^1.2.3 || ^4.0",
"yoast/phpunit-polyfills": "^1.0"
},
"suggest": {
"aws/aws-php-sns-message-validator": "To validate incoming SNS notifications",
"doctrine/cache": "To use the DoctrineCacheAdapter",
"ext-curl": "To send requests using cURL",
"ext-openssl": "Allows working with CloudFront private distributions and verifying received SNS messages",
"ext-sockets": "To use client-side monitoring"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"files": [
"src/functions.php"
],
"psr-4": {
"Aws\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Amazon Web Services",
"homepage": "http://aws.amazon.com"
}
],
"description": "AWS SDK for PHP - Use Amazon Web Services in your PHP project",
"homepage": "http://aws.amazon.com/sdkforphp",
"keywords": [
"amazon",
"aws",
"cloud",
"dynamodb",
"ec2",
"glacier",
"s3",
"sdk"
],
"support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues",
"source": "https://github.com/aws/aws-sdk-php/tree/3.300.12"
},
"time": "2024-03-06T19:38:08+00:00"
},
{ {
"name": "brick/math", "name": "brick/math",
"version": "0.11.0", "version": "0.11.0",
@ -1714,6 +1863,71 @@
], ],
"time": "2023-04-11T18:11:47+00:00" "time": "2023-04-11T18:11:47+00:00"
}, },
{
"name": "league/flysystem-aws-s3-v3",
"version": "3.24.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/flysystem-aws-s3-v3.git",
"reference": "809474e37b7fb1d1f8bcc0f8a98bc1cae99aa513"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/flysystem-aws-s3-v3/zipball/809474e37b7fb1d1f8bcc0f8a98bc1cae99aa513",
"reference": "809474e37b7fb1d1f8bcc0f8a98bc1cae99aa513",
"shasum": ""
},
"require": {
"aws/aws-sdk-php": "^3.295.10",
"league/flysystem": "^3.10.0",
"league/mime-type-detection": "^1.0.0",
"php": "^8.0.2"
},
"conflict": {
"guzzlehttp/guzzle": "<7.0",
"guzzlehttp/ringphp": "<1.1.1"
},
"type": "library",
"autoload": {
"psr-4": {
"League\\Flysystem\\AwsS3V3\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Frank de Jonge",
"email": "info@frankdejonge.nl"
}
],
"description": "AWS S3 filesystem adapter for Flysystem.",
"keywords": [
"Flysystem",
"aws",
"file",
"files",
"filesystem",
"s3",
"storage"
],
"support": {
"source": "https://github.com/thephpleague/flysystem-aws-s3-v3/tree/3.24.0"
},
"funding": [
{
"url": "https://ecologi.com/frankdejonge",
"type": "custom"
},
{
"url": "https://github.com/frankdejonge",
"type": "github"
}
],
"time": "2024-01-26T18:43:21+00:00"
},
{ {
"name": "league/mime-type-detection", "name": "league/mime-type-detection",
"version": "1.11.0", "version": "1.11.0",
@ -1871,6 +2085,72 @@
], ],
"time": "2023-02-06T13:46:10+00:00" "time": "2023-02-06T13:46:10+00:00"
}, },
{
"name": "mtdowling/jmespath.php",
"version": "2.7.0",
"source": {
"type": "git",
"url": "https://github.com/jmespath/jmespath.php.git",
"reference": "bbb69a935c2cbb0c03d7f481a238027430f6440b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jmespath/jmespath.php/zipball/bbb69a935c2cbb0c03d7f481a238027430f6440b",
"reference": "bbb69a935c2cbb0c03d7f481a238027430f6440b",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0",
"symfony/polyfill-mbstring": "^1.17"
},
"require-dev": {
"composer/xdebug-handler": "^3.0.3",
"phpunit/phpunit": "^8.5.33"
},
"bin": [
"bin/jp.php"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.7-dev"
}
},
"autoload": {
"files": [
"src/JmesPath.php"
],
"psr-4": {
"JmesPath\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Declaratively specify how to extract elements from a JSON document",
"keywords": [
"json",
"jsonpath"
],
"support": {
"issues": "https://github.com/jmespath/jmespath.php/issues",
"source": "https://github.com/jmespath/jmespath.php/tree/2.7.0"
},
"time": "2023-08-25T10:54:48+00:00"
},
{ {
"name": "nesbot/carbon", "name": "nesbot/carbon",
"version": "2.66.0", "version": "2.66.0",

2
package-lock.json generated
View file

@ -1,5 +1,5 @@
{ {
"name": "filesharing", "name": "filesharing-contribute",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {

View file

@ -7,10 +7,10 @@
resolved "https://registry.npmjs.org/@alloc/quick-lru/-/quick-lru-5.2.0.tgz" resolved "https://registry.npmjs.org/@alloc/quick-lru/-/quick-lru-5.2.0.tgz"
integrity sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw== integrity sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==
"@esbuild/darwin-arm64@0.17.18": "@esbuild/win32-x64@0.17.18":
version "0.17.18" version "0.17.18"
resolved "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.17.18.tgz" resolved "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.17.18.tgz"
integrity sha512-6tY+djEAdF48M1ONWnQb1C+6LiXrKjmqjzPNPWXhu/GzOHTHX2nh8Mo2ZAmBFg0kIodHhciEgUBtcYCAIjGbjQ== integrity sha512-qU25Ma1I3NqTSHJUOKi9sAH1/Mzuvlke0ioMJRthLXKm7JiSKVwFghlGbDLOO2sARECGhja4xYfRAZNPAkooYg==
"@jridgewell/gen-mapping@^0.3.2": "@jridgewell/gen-mapping@^0.3.2":
version "0.3.3" version "0.3.3"
@ -382,11 +382,6 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz" resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==
fsevents@~2.3.2:
version "2.3.2"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
function-bind@^1.1.1: function-bind@^1.1.1:
version "1.1.1" version "1.1.1"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz" resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz"