Ejemplo n.º 1
0
 /**
  * Upload an asset.
  * @throws PageblokException
  */
 public function uploadAsset()
 {
     $date = \Carbon::today();
     $file = \Input::file('file');
     $originalName = $file->getClientOriginalName();
     $extension = $file->getClientOriginalExtension();
     // if extension cannot be found a negative 4 is given
     $extensionPosition = strpos($originalName, '.' . $extension) ?: -4;
     $basename = \Str::slug(substr($file->getClientOriginalName(), 0, $extensionPosition));
     // we always save it like .jpg
     $fileName = $date->toDateString() . '-' . $basename . '.jpg';
     if (!$file->isValid()) {
         throw new PageblokException("No file to upload!", 12);
     }
     $uploadedFilePath = \Pageblok::getUploadPath() . '/' . $fileName;
     \Image::make($file)->save($uploadedFilePath, 75);
     // check if file exists
     if (\File::get($uploadedFilePath)) {
         // file url is the relative url
         $fileUrl = '/' . \Config::get('pageblok::settings.upload.folder') . '/' . $fileName;
         return \Response::json(array('status' => true, 'message' => $fileUrl));
     }
     return \Response::json(array('status' => false, 'message' => 'Error during upload'));
 }