3 Commits
3f24919507
...
cfc8ec2616
Author | SHA1 | Message | Date |
---|---|---|---|
Mohammad Khazaee | cfc8ec2616 |
change save file to buffer
|
2 years ago |
Mohammad Khazaee | 66090628db |
change save file to buffer
|
2 years ago |
Mohammad Khazaee | 47d395be84 |
test for modella project
|
2 years ago |
22 changed files with 270 additions and 466 deletions
-
1.gitignore
-
97app/Http/Controllers/FileController.php
-
2app/Http/Middleware/BindFileModelMiddleware.php
-
2app/Http/Requests/FileStoreRequest.php
-
117app/Image/ImageProcessor.php
-
5composer.json
-
47composer.lock
-
6database/factories/BaseFactory.php
-
18database/factories/CollectionFactory.php
-
2database/factories/Documents/PolicyDocumentFactory.php
-
28database/factories/FileFactory.php
-
2database/factories/ILaravelFactory.php
-
2database/factories/UserFactory.php
-
111routes/web.php
-
BINstorage/stub/image1.png
-
8tests/Base/FactoryMethodsTrait.php
-
53tests/Feature/FileDeleteTest.php
-
47tests/Feature/FileRestoreTest.php
-
29tests/Feature/FileShowTest.php
-
102tests/Feature/FileStoreTest.php
-
49tests/Feature/FileUpdateTest.php
@ -1,97 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\Http\Controllers; |
|
||||
|
|
||||
use App\Http\Requests\FileStoreRequest; |
|
||||
use App\Http\Requests\FileUpdateRequest; |
|
||||
use App\Http\Resources\FileResource; |
|
||||
use App\Image\ImageProcessor; |
|
||||
use App\Jobs\FileConversionQueue; |
|
||||
use App\Models\File; |
|
||||
use Illuminate\Http\Request; |
|
||||
use Illuminate\Support\Facades\DB; |
|
||||
use Illuminate\Support\Facades\Storage; |
|
||||
use Illuminate\Support\Facades\Validator; |
|
||||
use Symfony\Component\Console\Input\Input; |
|
||||
|
|
||||
use function PHPUnit\Framework\isNull; |
|
||||
|
|
||||
class FileController extends Controller |
|
||||
{ |
|
||||
public function show($collection, $uuid, $ext, Request $request, ImageProcessor $imageProcessor) |
|
||||
{ |
|
||||
if ($request->castParams == 'resource') { |
|
||||
return new FileResource(app()->file); |
|
||||
} |
|
||||
if (!is_null(array_intersect(array_keys($request->all()), $this->availableParams))) { |
|
||||
$imageProcessor->process(app()->file->getPath(), app()->file->getModifiedPath(), $request->all()); |
|
||||
return response()->file(app()->file->getModifiedPath()); |
|
||||
} |
|
||||
return response()->file(app()->file->getPath()); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
// for local private we didn't talk about it
|
|
||||
// public function private($collection, $path)
|
|
||||
// {
|
|
||||
// Storage::disk('local')->download($path);
|
|
||||
// }
|
|
||||
|
|
||||
public function store(FileStoreRequest $request, ImageProcessor $imageProcessor) |
|
||||
{ |
|
||||
$request->file = $imageProcessor->convertImage($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext); |
|
||||
if (!is_null(app()->collection->process)) { |
|
||||
$request->file = $imageProcessor->process($request->file->path(), '/tmp/' . app()->uuid . '.' . app()->collection->ext, app()->collection->process); |
|
||||
} |
|
||||
|
|
||||
$fileResource = null; |
|
||||
DB::transaction(function () use ($request, &$fileResource) { |
|
||||
$uuid = app()->uuid; |
|
||||
$fileResource = File::create([ |
|
||||
'uuid' => $uuid, |
|
||||
'original_name' => $request->name, |
|
||||
'public' => $request->public, |
|
||||
'ext' => $request->file->extension(), |
|
||||
'mimetype' => $request->file->getMimeType(), |
|
||||
'width' => getimagesize($request->file)[0], |
|
||||
'height' => getimagesize($request->file)[1], |
|
||||
'file_size' => $request->file->getSize(), |
|
||||
'sort' => $request->file->getSize(), |
|
||||
'alts' => $request->alts, |
|
||||
'description' => $request->description, |
|
||||
'user_id' => auth()->id(), |
|
||||
'ip' => $request->ip(), |
|
||||
'collection_id' => app()->collection->id, |
|
||||
'published_at' => $request->published_at, |
|
||||
'server_path' => '/' . date('y') . '/' . date('m') . '/', |
|
||||
]); |
|
||||
if (!app()->collection->tmp_support && app()->collection->count == 1) { |
|
||||
File::where('user_id', auth()->id())->delete(); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
$storedFile = Storage::disk(app()->collection->disk)->putFileAs($fileResource->server_path, $request->file, $fileResource->uuid . '.' . app()->collection->ext); |
|
||||
|
|
||||
if (app()->collection->public) { |
|
||||
Storage::disk(app()->collection->disk)->setVisibility($storedFile, 'public'); |
|
||||
} |
|
||||
|
|
||||
}); |
|
||||
|
|
||||
return new FileResource($fileResource); |
|
||||
} |
|
||||
|
|
||||
public function update(FileUpdateRequest $request) |
|
||||
{ |
|
||||
app()->file->update($request->all()); |
|
||||
return new FileResource(app()->file); |
|
||||
} |
|
||||
|
|
||||
public function destroy() |
|
||||
{ |
|
||||
if (app()->file->trashed()) { |
|
||||
return app()->file->restore(); |
|
||||
} |
|
||||
return app()->file->delete(); |
|
||||
} |
|
||||
} |
|
Binary file not shown.
Before Width: 600 | Height: 1200 | Size: 620 KiB |
@ -1,47 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace Tests\Feature; |
|
||||
|
|
||||
use App\Image\ImageProcessor; |
|
||||
use App\Models\Collection; |
|
||||
use App\Models\File; |
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase; |
|
||||
use Illuminate\Foundation\Testing\WithFaker; |
|
||||
use Illuminate\Support\Facades\Storage; |
|
||||
use Tests\TestCase; |
|
||||
|
|
||||
class FileRestoreTest extends TestCase |
|
||||
{ |
|
||||
public function test_user_with_permission_can_not_restore_file() |
|
||||
{ |
|
||||
$this->assertFalse("it's not mohammad's fault, I'm waiting for dynamic policy"); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
public function test_user_with_permission_can_restore_file() |
|
||||
{ |
|
||||
$collection = Collection::factory()->createQuietly([ |
|
||||
'alt_required' => false, |
|
||||
'description_required' => false, |
|
||||
'tmp_support' => true, |
|
||||
'max_width' => 2000, |
|
||||
'max_height' => 2000, |
|
||||
'min_width' => 1, |
|
||||
'min_height' => 1, |
|
||||
'min_file_size' => 0 |
|
||||
]); |
|
||||
$uuid = app()->uuid; |
|
||||
$file = File::factory()->createQuietly([ |
|
||||
'uuid' => $uuid, |
|
||||
'server_path' => '/' . date('y') . '/' . date('m') . '/', |
|
||||
'user_id' => auth()->id(), |
|
||||
'collection_id' => $collection->id |
|
||||
]); |
|
||||
$imageProcessor = new ImageProcessor; |
|
||||
$imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext)); |
|
||||
|
|
||||
$file->delete(); |
|
||||
$response = $this->loginAs()->deleteJson(route('api.files.destroy', ['collection_name' => $collection->name, 'uuid' => $file->uuid, 'extention' => $collection->ext])); |
|
||||
$response->assertok(); |
|
||||
} |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue