protected function checkUploadAction() { $uploadedFile = Input::file('file_data'); if (!Request::isMethod('POST') || !is_object($uploadedFile)) { return; } $validationRules = []; /* * Validate file types */ if (count($this->fileTypes)) { $mimes = trim(implode(',', (array) $this->fileTypes)); $mimes = str_replace('.', '', $mimes); // ['mimes:png,jpg,jpeg']; if ($mimes != '*') { $validationRules[] = 'mimes:' . $mimes; } } $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new ApplicationException(sprintf('File %s is not valid.', $uploadedFile->getClientOriginalName())); } $file = new File(); $file->data = $uploadedFile; $file->is_public = true; $file->save(); $this->model->{$this->attribute}()->add($file, $this->getSessionKey()); return ['id' => $file->id, 'path' => $file->getPath()]; }
/** * Save the first image result from Google. * * @param string $query Search query * @param int $height Resize height * @param int $width Resize width * @param string $path Path to store the image (will use temp as default) * * @return System\Models\File|void Created file object */ public static function search($query, $height = 300, $width = 300, $path = null) { // Prepare the query $query = urlencode($query); // Prepare path $path = $path ?: tempnam(sys_get_temp_dir(), 'image'); // Use Google Image $results = file_get_contents('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' . $query); if ($results && ($json = json_decode($results, true))) { foreach ($json['responseData']['results'] as $image) { if (isset($image['url'])) { // Retrive the image $url = $image['url']; $content = @file_get_contents($url); if (!$content) { break; } // Save the image $file_path = $path . basename($url); file_put_contents($file_path, $content); // Resize Resizer::open($file_path)->resize($height, $width)->save($file_path, 100); // Create File object $file = new File(); $file->data = $file_path; $file->save(); return $file; } } } }
/** * Checks the current request to see if it is a postback containing a file upload * for this particular widget. */ protected function checkUploadPostback() { if (!($uniqueId = Request::header('X-OCTOBER-FILEUPLOAD')) || $uniqueId != $this->getId()) { return; } // Adds the path to the lang files app('translator')->getLoader()->addNamespace('patroklo.formwidgets', base_path('vendor/patroklo/octobercms-improved-fileupload/src/lang')); try { if (!Input::hasFile('file_data')) { throw new ApplicationException('File missing from request'); } $uploadedFile = Input::file('file_data'); // Load the $this->rules parameter $this->setValidationRules(); $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $this->rules], ['max_files' => e(trans('patroklo.formwidgets::messages.max_files'))]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new ApplicationException('File is not valid'); } $fileRelation = $this->getRelationObject(); $file = new File(); $file->data = $uploadedFile; $file->is_public = $fileRelation->isPublic(); $file->save(); $fileRelation->add($file, $this->sessionKey); $file = $this->decorateFileAttributes($file); $result = ['id' => $file->id, 'thumb' => $file->thumbUrl, 'path' => $file->pathUrl]; Response::json($result, 200)->send(); } catch (Exception $ex) { Response::json($ex->getMessage(), 400)->send(); } exit; }
private function processFileUploadPhoto($id, $uploadedFile) { $file = new System\Models\File(); $file->data = $uploadedFile; $file->save(); $mhs = PendaftaranWisuda::find($id); $mhs->photo_ijazah()->add($file); }
public function run() { $driver = Driver::firstOrCreate(['name' => 'U.S. Postal Service', 'type' => 'shipping', 'class' => 'Bedard\\USPS\\Classes\\USPS', 'is_configurable' => true, 'is_default' => false]); $logo = new File(); $logo->fromFile(plugins_path('bedard/usps/assets/images/usps.png')); $logo->save(); $driver->image()->add($logo); }
private function processFileUpload($id, $uploadedFile) { $file = new System\Models\File(); $file->data = $uploadedFile; $file->save(); $mhs = IdentitasMahasiswa::find($id); $mhs->photo()->add($file); }
public function onAvatarUpdate() { $file = new File(); $file->data = Input::file('avatar'); $file->save(); $this->AvaUser->avatar()->add($file); return Redirect::intended($this->pageUrl($this->property('redirect'))); }
public function postUpload($id = '') { $user = Auth::getUser(); $record = Ad::whereRaw('id = ? AND user_id = ?', array($id, $user->id))->first(); if (!$record) { return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'user_not_perm')), 500); } // Check limit photos $count = File::whereRaw('attachment_id = ? AND attachment_type = ?', array($id, 'DLNLab\\Classified\\Models\\Ad'))->count(); if ($count >= CLF_LIMIT_AD_PHOTO) { return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'user_not_perm')), 500); } $result = null; if (Input::hasFile('file_data')) { try { $uploadedFile = Input::file('file_data'); $validationRules = ['max:' . File::getMaxFilesize()]; $validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif'; $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new SystemException('File is not valid'); } $file = new File(); $file->data = $uploadedFile; $file->field = 'ad_images'; $file->attachment_id = $id; $file->attachment_type = 'DLNLab\\Classified\\Models\\Ad'; $file->is_public = true; $file->save(); $file->thumb = $file->getThumb(250, 250, ['mode' => 'crop']); $result = new \stdClass(); $result->id = $file->id; } catch (Exception $ex) { $result = $ex->getMessage(); return Response::json(array('status' => 'error', 'message' => $result), 500); } } $result->photo_pattern = '<div id="dln_photo_item_' . $file->id . '" data-id="' . $file->id . '" class="dln-photo-item panel panel-default bg-master-lightest sm-m-b-5 sm-m-l-5 sm-m-r-5"> <div class="panel-body sm-p-t-10 sm-p-l-5 sm-p-r-5 sm-p-b-10"> <div class="col-xs-12 col-sm-3 sm-p-l-5 sm-p-r-5"> <img class="img-thumbnail m-b-5" width="100%" src="' . $file->thumb . '"> </div> <div class="col-xs-12 col-sm-9 sm-p-l-5 sm-p-r-5"> <textarea placeholder="' . trans(CLF_LANG_LABEL . 'noi_dung') . '" id="dln_photo_desc" class="form-control clearfix m-b-10" required maxlength="500">' . $file->desc . '</textarea> <a href="javascript:void(0)" class="dln-up-photo btn btn-sm btn-default pull-left m-r-5" data-original-title="' . trans(CLF_LANG_LABEL . 'len') . '" data-toggle="tooltip"><i class="fs-14 fa fa-arrow-up"></i></a> <a href="javascript:void(0)" class="dln-down-photo btn btn-sm btn-default pull-left" data-original-title="' . trans(CLF_LANG_LABEL . 'xuong') . '" data-toggle="tooltip"><i class="fs-14 fa fa-arrow-down"></i></a> <a href="javascript:void(0)" class="dln-delete-photo btn btn-sm btn-danger pull-right" data-original-title="' . trans(CLF_LANG_LABEL . 'xoa') . '" data-toggle="tooltip"><i class="fs-14 fa fa-trash-o"></i></a> </div> </div> </div>'; return Response::json(response_message(200, $result)); }
protected function checkUploadPostback() { if (!post('X_BLOG_IMAGE_UPLOAD')) { return; } $uploadedFileName = null; try { $uploadedFile = Input::file('file'); if ($uploadedFile) { $uploadedFileName = $uploadedFile->getClientOriginalName(); } $validationRules = ['max:' . File::getMaxFilesize()]; $validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif'; $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new SystemException(Lang::get('cms::lang.asset.file_not_valid')); } $fileRelation = $this->model->content_images(); $file = new File(); $file->data = $uploadedFile; $file->is_public = true; $file->save(); $fileRelation->add($file, $this->sessionKey); $result = ['file' => $uploadedFileName, 'path' => $file->getPath()]; $response = Response::make()->setContent($result); $response->send(); die; } catch (Exception $ex) { $message = $uploadedFileName ? Lang::get('cms::lang.asset.error_uploading_file', ['name' => $uploadedFileName, 'error' => $ex->getMessage()]) : $ex->getMessage(); $result = ['error' => $message, 'file' => $uploadedFileName]; $response = Response::make()->setContent($result); $response->send(); die; } }
/** * File upload controller */ public function post_files() { try { if (!Input::hasFile('file')) { throw new ApplicationException('No file in request'); } $upload = Input::file('file'); $validationRules = ['max:' . File::getMaxFilesize()]; $validation = Validator::make(['file' => $upload], ['file' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$upload->isValid()) { throw new ApplicationException(sprintf('File %s is not valid.', $upload->getClientOriginalName())); } $file = new File(); $file->data = $upload; $file->is_public = true; $file->save(); return Response::json(['id' => $file->id], 200); } catch (Exception $e) { return Response::json($e->getMessage(), 400); } }
/** * Checks the current request to see if it is a postback containing a file upload * for this particular widget. */ protected function checkUploadPostback() { if (!($uniqueId = post('X_OCTOBER_FILEUPLOAD')) || $uniqueId != $this->getId()) { return; } try { $uploadedFile = Input::file('file_data'); $isImage = starts_with($this->getDisplayMode(), 'image'); $validationRules = ['max:' . File::getMaxFilesize()]; if ($isImage) { $validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif'; } $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new SystemException('File is not valid'); } $fileRelation = $this->getRelationObject(); $file = new File(); $file->data = $uploadedFile; $file->is_public = $fileRelation->isPublic(); $file->save(); $fileRelation->add($file, $this->sessionKey); $file->thumb = $file->getThumb($this->imageWidth, $this->imageHeight, ['mode' => 'crop']); $result = $file; } catch (Exception $ex) { $result = json_encode(['error' => $ex->getMessage()]); } die($result); }
/** * Checks the current request to see if it is a postback containing a file upload * for this particular widget. */ protected function checkUploadPostback() { if (!($uniqueId = Request::header('X-OCTOBER-FILEUPLOAD')) || $uniqueId != $this->getId()) { return; } try { if (!Input::hasFile('file_data')) { throw new ApplicationException('File missing from request'); } $uploadedFile = Input::file('file_data'); $validationRules = ['max:' . File::getMaxFilesize()]; if ($fileTypes = $this->getAcceptedFileTypes()) { $validationRules[] = 'extensions:' . $fileTypes; } if ($this->mimeTypes) { $validationRules[] = 'mimes:' . $this->mimeTypes; } $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new ApplicationException('File is not valid'); } $fileRelation = $this->getRelationObject(); $file = new File(); $file->data = $uploadedFile; $file->is_public = $fileRelation->isPublic(); $file->save(); $fileRelation->add($file, $this->sessionKey); $file = $this->decorateFileAttributes($file); $result = ['id' => $file->id, 'thumb' => $file->thumbUrl, 'path' => $file->pathUrl]; Response::json($result, 200)->send(); } catch (Exception $ex) { Response::json($ex->getMessage(), 400)->send(); } exit; }
public static function getUserAvatar($uid = '', $image_url = '') { if (!$image_url || !$uid) { return false; } // Save user avatar $avatar = File::whereRaw('attachment_type = ? AND attachment_id = ?', array('RainLab\\User\\Models\\User', $uid))->first(); if (empty($avatar)) { $file_name = $uid . '.jpg'; $temp_name = CLF_UPLOAD . $file_name; $data = @file_get_contents($image_url); $success = @file_put_contents($temp_name, $data); $file = new File(); $file->data = $temp_name; $file->field = 'avatar'; $file->file_name = $file_name; $file->attachment_id = $uid; $file->attachment_type = 'RainLab\\User\\Models\\User'; $file->is_public = true; $file->save(); @unlink($file_name); } }
protected function checkUploadAction() { if (!($uniqueId = Request::header('X-OCTOBER-FILEUPLOAD')) || $uniqueId != $this->alias) { return; } try { if (!Input::hasFile('file_data')) { throw new ApplicationException('File missing from request'); } $uploadedFile = Input::file('file_data'); $validationRules = ['max:' . File::getMaxFilesize()]; if ($fileTypes = $this->processFileTypes()) { $validationRules[] = 'extensions:' . $fileTypes; } $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new ApplicationException(sprintf('File %s is not valid.', $uploadedFile->getClientOriginalName())); } $file = new File(); $file->data = $uploadedFile; $file->is_public = true; $file->save(); $this->model->{$this->attribute}()->add($file, $this->getSessionKey()); $file = $this->decorateFileAttributes($file); $result = ['id' => $file->id, 'thumb' => $file->thumbUrl, 'path' => $file->pathUrl]; return Response::json($result, 200); } catch (Exception $ex) { return Response::json($ex->getMessage(), 400); } }
/** * Create file from path, save it and return File object * * @param $path * @param $public * * @return File */ protected function getSavedFile($path, $public = true) { $file = new File(); $file->is_public = $public; $file->fromFile($path); $file->save(); return $file; }
/** * Checks the current request to see if it is a postback containing a file upload * for this particular widget. */ protected function checkUploadPostback() { if (!($uniqueId = post('X_OCTOBER_FILEUPLOAD')) || $uniqueId != $this->getId()) { return; } try { $uploadedFile = Input::file('file_data'); $validationRules = ['max:' . File::getMaxFilesize()]; if ($fileTypes = $this->getAcceptedFileTypes()) { $validationRules[] = 'mimes:' . $fileTypes; } $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new SystemException('File is not valid'); } $fileRelation = $this->getRelationObject(); $file = new File(); $file->data = $uploadedFile; $file->is_public = $fileRelation->isPublic(); $file->save(); $fileRelation->add($file, $this->sessionKey); $file->thumb = $file->getThumb($this->imageWidth, $this->imageHeight, $this->thumbOptions); $result = $file; } catch (Exception $ex) { $result = json_encode(['error' => $ex->getMessage()]); } header('Content-Type: application/json'); die($result); }
/** * Upload Avatar from Base64 encoded image * * @param \RainLab\User\Models\User $user * @param string $source * string contend of an image on Base64 enconding */ public static function uploadAvatarFromString($user, $source) { $dst = '/tmp/avatar_' . $user->getKey() . '_' . uniqid(); FileHelper::put($dst, base64_decode($source)); $validImage = true; try { // Validated is a JPG or PNG $imageType = exif_imagetype($dst); $validImage = in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF]); // Validated is not bigger that xx by xx if ($validImage) { // Test if image is corrupted if OctoberCMS Resizer can open it // is more likely the image is ok Resizer::open($dst); // Test image dimensions list($width, $height, $type, $attr) = getimagesize($dst); $validImage = $width <= 400 && $height <= 400; } // Add right file extension to the upload file if ($validImage) { // Save image with correct image extension $extension = [IMAGETYPE_JPEG => 'jpg', IMAGETYPE_PNG => 'png', IMAGETYPE_GIF => 'gif'][$imageType]; $newDst = $dst . '.' . $extension; rename($dst, $newDst); $dst = $newDst; } } catch (\Exception $e) { $validImage = false; } if (!$validImage) { throw new \Exception('Must be a valid JPG, GIF or PNG. And not bigger that 400x400 pixels.'); } $file = new File(); $file->data = $dst; $file->is_public = true; $file->save(); if ($file) { $user->avatar()->add($file); } }