public function uploadPost(Request $request, Folder $folder) { $this->authorize('all', $folder); $file = $request->file('file'); if (!$file) { fmsgs(['title' => 'No File Detected', 'type' => 'error', 'text' => 'Please provide the file you want to upload!']); return redirect()->back(); } $extension = $file->getClientOriginalExtension(); if (!$file->isValid()) { fmsgs(['title' => 'File is not Valid', 'type' => 'error', 'text' => $file->getErrorMessage()]); return redirect()->back(); } $filename = $file->getFilename() . '.' . $extension; $name = $file->getClientOriginalName(); $storage = Storage::disk('local')->put($filename, file_get_contents($file->getRealPath())); $file = FileModel::create(['name' => $name, 'filename' => $filename, 'folder_id' => $folder->id]); if (explode('/', fileInfo($file)['type'])[0] === 'image' && explode('/', mimeTypeFromString($extension))[0] === 'image') { $this->createOptimizedImage($filename); } if ($this->user->auto_share) { $this->_share($file, 1, true, false); } fmsgs(['title' => 'File Uploaded', 'type' => 'success', 'text' => 'The file uploaded successfully']); return redirect('/files/' . $folder->id); }
/** * @link http://api.nette.org/2.0/source-Templating.Helpers.php.html#266 * @param string * @return string */ function generateDataUri($data) { if (!is_string($data)) { throw new Exception('File error'); } $type = mimeTypeFromString($data); return 'data:' . ($type ? "{$type};" : '') . 'base64,' . base64_encode($data); }