Skip to content

Commit

Permalink
Prohibit extension change when renaming a file
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienheyd committed Nov 3, 2021
1 parent 53f0f23 commit 4e4c5e5
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 28 deletions.
21 changes: 19 additions & 2 deletions src/Controllers/MediaManagerController.php
Expand Up @@ -9,9 +9,11 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
use Image;
use Sebastienheyd\BoilerplateMediaManager\Models\Breadcrumb;
use Sebastienheyd\BoilerplateMediaManager\Models\File;
use Sebastienheyd\BoilerplateMediaManager\Models\Path;
use UnexpectedValueException;
use Validator;
Expand Down Expand Up @@ -209,12 +211,27 @@ public function paste(Request $request)
*
* @param Request $request
*
* @return JsonResponse|ResponseFactory
* @return JsonResponse
*/
public function rename(Request $request)
{
if(! $request->isXmlHttpRequest()) {
abort(403);
}

$validator = Validator::make($request->post(), [
'path' => 'required',
'type' => ['required', Rule::in(['folder', 'file'])],
'fileName' => 'required',
'newName' => 'required',
]);

if ($validator->fails()) {
return response()->json(['status' => 'error', 'message' => $validator->errors()->first()]);
}

try {
$path = new Path($request->input('path'));
$path = new Path($request->post('path'));
$path->rename($request->input('fileName'), $request->input('newName'));

return response()->json(['status' => 'success']);
Expand Down
27 changes: 16 additions & 11 deletions src/Models/File.php
Expand Up @@ -34,6 +34,9 @@ public function __construct($file)
*/
public function rename($newName)
{
$newName = preg_replace('#\.'.$this->pathinfo['extension'].'$#i', '', $newName);
$newName = $newName.'.'.$this->pathinfo['extension'];

foreach ($this->getThumbs() as $thumb) {
$this->storage->move($thumb['fullpath'], $thumb['dirname'].'/'.$newName);
}
Expand Down Expand Up @@ -186,17 +189,19 @@ public function toArray()
$ts = filemtime($this->getFullPath());

return [
'download' => '',
'icon' => $this->getIcon(),
'thumb' => $this->getThumbUrl().'?'.$ts,
'type' => $this->detectFileType(),
'name' => basename($this->file),
'isDir' => false,
'size' => $this->getFilesize(),
'link' => route('mediamanager.index', ['path' => $this->file], false),
'url' => $this->storage->url($this->file).'?'.$ts,
'time' => $this->getFileChangeTime(),
'ts' => $ts,
'download' => '',
'icon' => $this->getIcon(),
'thumb' => $this->getThumbUrl().'?'.$ts,
'type' => $this->detectFileType(),
'name' => basename($this->file),
'filename' => $this->pathinfo['filename'],
'extension' => $this->pathinfo['extension'],
'isDir' => false,
'size' => $this->getFilesize(),
'link' => route('mediamanager.index', ['path' => $this->file], false),
'url' => $this->storage->url($this->file).'?'.$ts,
'time' => $this->getFileChangeTime(),
'ts' => $ts,
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/public/mediamanager.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e4c5e5

Please sign in to comment.