Skip to content

Commit 0433dca

Browse files
author
dante
committed
Added update file api
1 parent b78a7c9 commit 0433dca

3 files changed

Lines changed: 102 additions & 9 deletions

File tree

‎ProcessMaker/Http/Controllers/Api/FileController.php‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Laravel\Horizon\Http\Controllers\Controller;
88
use ProcessMaker\Http\Resources\ApiCollection;
99
use ProcessMaker\Models\Media;
10-
use ProcessMaker\Models\User;
1110
use Spatie\BinaryUuid\HasBinaryUuid;
1211
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1312

@@ -72,9 +71,14 @@ public function store(Request $request)
7271
throw new NotFoundHttpException();
7372
}
7473

75-
$model->addMediaFromRequest('file')->toMediaCollection('local');
74+
$addedMedia = $model->addMediaFromRequest('file')->toMediaCollection('local');
7675

77-
return response([], 201);
76+
return response([
77+
'uuid' => $addedMedia->uuid_text,
78+
'model_id' => $addedMedia->model_id_text,
79+
'file_name' => $addedMedia->file_name,
80+
'mime_type' => $addedMedia->mime_type
81+
], 200);
7882
}
7983

8084
/**
@@ -116,7 +120,10 @@ public function edit($id)
116120
*/
117121
public function update(Request $request, Media $file)
118122
{
119-
//
123+
$newFile = $request->file('file');
124+
$newMedia = new \ProcessMaker\Media();
125+
$newMedia->updateFile($newFile, $file);
126+
return response([], 201);
120127
}
121128

122129
/**

‎ProcessMaker/Media.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,58 @@
22

33
namespace ProcessMaker;
44

5+
use Illuminate\Http\File;
6+
use Illuminate\Http\UploadedFile;
7+
use Illuminate\Support\Facades\Storage;
58
use Spatie\BinaryUuid\HasBinaryUuid;
9+
use Spatie\MediaLibrary\FileManipulator;
10+
use Spatie\MediaLibrary\Filesystem\Filesystem;
611
use Spatie\MediaLibrary\Models\Media as BaseMedia;
712

813
class Media extends BaseMedia
914
{
1015
use HasBinaryUuid;
16+
17+
/**
18+
* The binary UUID attributes that should be converted to text.
19+
*
20+
* @var array
21+
*/
22+
protected $uuids = [
23+
'model_id'
24+
];
25+
26+
/**
27+
* Updates the Media with a new file
28+
*
29+
* @param UploadedFile $newFile
30+
* @param Models\Media $file
31+
*/
32+
public function updateFile(UploadedFile $newFile, \ProcessMaker\Models\Media $file)
33+
{
34+
$originalFilePath = $file->uuid_text . '/' . $file->file_name;
35+
$newFileName = $this->sanitizeFileName($newFile->getClientOriginalName());
36+
$newFilePath = $file->uuid_text . '/' . $newFileName;
37+
38+
Storage::disk('public')->delete($originalFilePath);
39+
Storage::disk('public')->put($newFilePath, $newFile);
40+
41+
$file->file_name = $newFileName;
42+
$file->name = pathinfo($newFileName, PATHINFO_FILENAME);
43+
$file->mime_type = $newFile->getMimeType();
44+
$file->size = filesize($newFile->path());
45+
$file->save();
46+
}
47+
48+
/**
49+
* Removes from file name non accepted characters
50+
*
51+
* @param string $fileName
52+
*
53+
* @return string
54+
*/
55+
protected function sanitizeFileName(string $fileName)
56+
{
57+
return str_replace(['#', '/', '\\'], '-', $fileName);
58+
}
1159
}

‎tests/Feature/Api/FilesTest.php‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Tests\Feature\Api;
44

5-
use Faker\Factory as Faker;
65
use Illuminate\Foundation\Testing\DatabaseTransactions;
7-
use Illuminate\Http\Request;
86
use Illuminate\Http\UploadedFile;
9-
use Illuminate\Support\Facades\Auth;
107
use Illuminate\Support\Facades\Storage;
8+
use ProcessMaker\Models\Media;
119
use ProcessMaker\Models\User;
10+
use Spatie\BinaryUuid\HasBinaryUuid;
1211
use Tests\TestCase;
1312
use Tests\Feature\Shared\ApiCallWithUser;
1413
use Illuminate\Support\Facades\Hash;
@@ -124,7 +123,7 @@ public function testGetFile()
124123
/**
125124
* Upload a file and associate it to a model
126125
*/
127-
public function testCreateFile()
126+
public function testCreateFile()
128127
{
129128
// We create a fake file to upload
130129
Storage::fake('public');
@@ -141,14 +140,53 @@ public function testCreateFile()
141140
$response = $this->actingAs($this->user, 'api')
142141
->json('POST', self::API_TEST_URL . '?model=user&model_uuid=' . $model->uuid_text, $data);
143142

143+
// Validate the header status code
144+
$response->assertStatus(200);
145+
146+
// Validate that a file was created in the media directory
147+
$mediaObj = json_decode($response->getContent());
148+
Storage::disk('public')->assertExists($mediaObj->uuid . '/test.txt');
149+
}
150+
151+
/**
152+
* Update a media with a new file
153+
*/
154+
public function testUpdateFile()
155+
{
156+
// We create a fake file to upload
157+
Storage::fake('public');
158+
$fileUploadInsert = UploadedFile::fake()->create('insertedFile.txt', 1);
159+
$fileUploadUpdate= UploadedFile::fake()->create('updatedFile.txt', 2);
160+
161+
// We create a model (in this case a user) to whom the file will be associated
162+
$model = factory(User::class)->create();
163+
$addedMedia = $model->addMedia($fileUploadInsert)->toMediaCollection('local');
164+
165+
// In the data array add the file to update
166+
$data = [
167+
'file' => $fileUploadUpdate
168+
];
169+
170+
$response = $this->actingAs($this->user, 'api')
171+
->json('PUT', self::API_TEST_URL . '/' . $addedMedia->uuid_text, $data);
172+
144173
// Validate the header status code
145174
$response->assertStatus(201);
175+
176+
// Validate that the file was updated in the directory of the inserted media
177+
Storage::disk('public')->assertExists($addedMedia->uuid_text . '/updatedFile.txt');
178+
179+
// Validate that the media table has been updated
180+
$modelId = HasBinaryUuid::encodeUuid($addedMedia->uuid_text);
181+
$updatedMediaModel = Media::find($modelId);
182+
$this->assertEquals('updatedFile.txt', $updatedMediaModel->file_name);
183+
$this->assertEquals('updatedFile', $updatedMediaModel->name);
146184
}
147185

148186
/**
149187
* Remove a file and its model associations
150188
*/
151-
public function testDestroyFile()
189+
public function testDestroyFile()
152190
{
153191
// We create a fake file to upload
154192
Storage::fake('public');

0 commit comments

Comments
 (0)