| @@ 25-56 (lines=32) @@ | ||
| 22 | * |
|
| 23 | * @author Beñat Espiña <[email protected]> |
|
| 24 | */ |
|
| 25 | trait OverwriteAction |
|
| 26 | { |
|
| 27 | /** |
|
| 28 | * Overwrite action. |
|
| 29 | * |
|
| 30 | * @param string $anId The file id |
|
| 31 | * @param Request $aRequest The request |
|
| 32 | * @param FileCommandBus $aCommandBus The command bus |
|
| 33 | * @param string $aProperty The file property that want to get from request |
|
| 34 | * |
|
| 35 | * @return array |
|
| 36 | */ |
|
| 37 | public function overwriteAction($anId, Request $aRequest, FileCommandBus $aCommandBus, $aProperty) |
|
| 38 | { |
|
| 39 | $uploadedFile = $aRequest->files->get($aProperty); |
|
| 40 | $command = new OverwriteFileCommand( |
|
| 41 | $anId, |
|
| 42 | $uploadedFile->getClientOriginalName(), |
|
| 43 | file_get_contents($uploadedFile->getPathname()), |
|
| 44 | $uploadedFile->getMimeType() |
|
| 45 | ); |
|
| 46 | $aCommandBus->handle($command); |
|
| 47 | ||
| 48 | $aRequest->files->remove($aProperty); |
|
| 49 | ||
| 50 | return [ |
|
| 51 | 'id' => $command->id(), |
|
| 52 | 'filename' => $uploadedFile->getClientOriginalName(), |
|
| 53 | 'mime_type' => $uploadedFile->getMimeType(), |
|
| 54 | ]; |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||
| @@ 24-53 (lines=30) @@ | ||
| 21 | * |
|
| 22 | * @author Beñat Espiña <[email protected]> |
|
| 23 | */ |
|
| 24 | trait UploadAction |
|
| 25 | { |
|
| 26 | /** |
|
| 27 | * Upload action. |
|
| 28 | * |
|
| 29 | * @param Request $aRequest The request |
|
| 30 | * @param FileCommandBus $aCommandBus The command bus |
|
| 31 | * @param string $aProperty The file property that want to get from request |
|
| 32 | * |
|
| 33 | * @return array |
|
| 34 | */ |
|
| 35 | public function uploadAction(Request $aRequest, FileCommandBus $aCommandBus, $aProperty) |
|
| 36 | { |
|
| 37 | $uploadedFile = $aRequest->files->get($aProperty); |
|
| 38 | $command = new UploadFileCommand( |
|
| 39 | $uploadedFile->getClientOriginalName(), |
|
| 40 | file_get_contents($uploadedFile->getPathname()), |
|
| 41 | $uploadedFile->getMimeType() |
|
| 42 | ); |
|
| 43 | $aCommandBus->handle($command); |
|
| 44 | ||
| 45 | $aRequest->files->remove($aProperty); |
|
| 46 | ||
| 47 | return [ |
|
| 48 | 'id' => $command->id(), |
|
| 49 | 'filename' => $uploadedFile->getClientOriginalName(), |
|
| 50 | 'mime_type' => $uploadedFile->getMimeType(), |
|
| 51 | ]; |
|
| 52 | } |
|
| 53 | } |
|
| 54 | ||