upload image with ajax in symfony2 - php

I want to click on an image opens the window for selecting a new image, and change the screen, now I want to change this picture in the bank, but this pending on an error:
Catchable Fatal Error: Argument 1 passed to
Delivve\WebBundle\Entity\User::setFile() must be an instance of
Symfony\Component\HttpFoundation\File\UploadedFile, string given,
called in
/home/delivve-webservice/src/Delivve/WebBundle/Controller/UserController.php
I do not know whether the error is in the path image that I'm taking?
$("#bundle_user_file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.active-img').attr('src', e.target.result);
ajax_formData(e.target.result);
};
reader.readAsDataURL(this.files[0]);
}
});
function ajax_formData(image) {
var path = "{{ path("submit_image_user", {"userId" : owner.id}) }}";
alert(image);
$.post(path, {image: image}, function (data) {
alert(data.message);
}, "json");
}
public function submitImageAction(Request $request, $userId){
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$entity = $this->getUser();
if ($entity->getId() != $userId) {
$response = new JsonResponse(
array(
'message' => "Não tem permissão de alterar esses dados"
), 400);
return $response;
}
if ($base64Content = $request->request->get('image')) {
$filePath = tempnam(sys_get_temp_dir(), 'UploadedFile');
$file = fopen($filePath, "w");
stream_filter_append($file, 'convert.base64-decode');
fwrite($file, $base64Content);
$meta_data = stream_get_meta_data($file);
$path = $meta_data['uri'];
fclose($file);
$entity->setFile($path);
$entity->upload();
$em->persist($entity);
$em->flush();
return new JsonResponse(array('message' => 'Success!'), 200);
}
$response = new JsonResponse(
array(
'message' => "imagem não encontrada"
), 400);
return $response;
}
these are the methods in my class
user.php
/**
* #return string
*/
public function getPictureUrl()
{
return $this->pictureUrl;
}
/**
* #param string $pictureUrl
*/
public function setPictureUrl($pictureUrl)
{
$this->pictureUrl = $pictureUrl;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Relative path.
* Get web path to upload directory.
*
* #return string
*/
public function getUploadPath()
{
return 'uploads/pictures';
}
/**
* Absolute path.
* Get absolute path to upload directory.
*
* #return string
*/
protected function getUploadAbsolutePath()
{
return __DIR__ . '/../../../../web/' . $this->getUploadPath();
}
/**
* Relative path.
* Get web path to a cover.
*
* #return null|string
*/
public function getPictureWeb()
{
return null === $this->getPictureUrl()
? null
: $this->getUploadPath() . '/' . $this->getPictureUrl();
}
/**
* Get path on disk to a cover.
*
* #return null|string
* Absolute path.
*/
public function getPictureAbsolute()
{
return null === $this->getPictureUrl()
? null
: $this->getUploadAbsolutePath() . '/' . $this->getPictureUrl();
}
/**
* Upload a cover file.
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
$filename = $this->getFile()->getClientOriginalName();
$this->getFile()->move($this->getUploadAbsolutePath(), $filename);
$this->setPictureUrl($filename);
$this->setFile();
}
the file I get comes from the type base64, I have to turn it into a uploadfile
$file = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAA..."
I would greatly appreciate it if someone could help me to do this submition of asynchronous file.

public function submitImageAction(Request $request, $userId)
{
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$entity = $this->getUser();
if ($entity->getId() != $userId) {
$response = new JsonResponse(
array(
'message' => "Não tem permissão de alterar esses dados"
), 400);
return $response;
}
if ($base64Content = $request->request->get('image')) {
$dat = preg_split("/,/", $base64Content);
if (($fileData = base64_decode($dat[1])) === false) {
$response = new JsonResponse(
array(
'message' => "Base64 decoding error."
), 400);
return $response;
}
$fileName = $entity->getUploadPath() . "/" . uniqid() . ".jpeg";
if (file_put_contents($fileName, $fileData)) {
$entity->setPictureUrl($fileName);
$em->persist($entity);
$em->flush();
return new JsonResponse(array('message' => 'Success!'), 200);
}
}
$response = new JsonResponse(
array(
'message' => "imagem não encontrada"
), 400);
return $response;
}
the error was in the driver only need to hit like sending the path to the User

Related

REST API - Different Behaviour on Different Servers

We have created a REST API PHP setup which is designed to compare the Tables of one database with another and if the user doesn't exist in one, create it in the Application. Also if there is a change to a user i.e. Email address, string etc then update it.
We've had a script created which using php works fantastically on our test server, it creates a user when required and updates them when required.
When we move this over to a different server which has the same database structure, same tables, same root username and password etc, it isn't recognising the existing users. It thinks it has xx,xxx users to create and then fails as 'user with that username already exists'.
Does any one have any ideas why this maybe the case? Below is the main php file which then has a config file, a db.php, a common.php and then a GpsGateapi.php
require_once('rest_api_includes/Config.php');
require_once(Config::API_INCLUDES_DIR . '/DB.php');
require_once(Config::API_INCLUDES_DIR . '/Gpsgate_API.php');
require_once(Config::API_INCLUDES_DIR . '/Common.php');
class API_Sync
{
/** #var int application id */
private $application_id;
/** #var int user type id */
private $user_type_id;
/** #var Gpsgate_API instance */
private $api;
/** #var DB instance */
private $db;
/** #var string<show|log> Show or log errors */
private $errors_report_type;
/** #var string<show|log|none> Show or log actions */
private $actions_report_type;
/**
* Sets variables, connects to database and REST API
*
* #throws \Exception
*/
public function __construct()
{
$this->application_id = Config::API_APPLIATION_ID;
$this->user_type_id = Config::API_USER_TYPE_ID;
$this->errors_report_type = Config::API_SHOW_OUTPUT ? 'show' : 'log';
$this->actions_report_type = Config::API_SHOW_OUTPUT ? 'show' : (Config::API_ACTIONS_LOGGING ? 'log' : 'none');
}
/**
* Main method that controlls all the process
*
* #throws \Exception
*/
public function run()
{
$this->prepare();
$gpsgate_users_result = $this->api->getUsers();
if ($gpsgate_users_result->status !== 200) {
throw new Exception(implode('; ', $gpsgate_users_result->body));
}
$gpsgate_users = $gpsgate_users_result->body;
$db_users = $this->db->get(DB::TBL_PERSONS);
$res = $this->compare($gpsgate_users, $db_users);
$this->updateUsers($res['update']);
$this->createUsers($res['create']);
}
/**
* Create gsgate users data from database array
*
* #param array $users
*/
private function createUsers($users)
{
if (!empty($users)) {
$this->logOrShowAction("Begin to create users data.");
foreach ($users as $user) {
$res = $this->api->createUser([
'email' => $user['Email'],
'name' => $user['FirstName'],
'surname' => $user['LastName'],
'driverID' => $user['RFID'],
'username' => $user['UserName'],
'password' => Common::generatePassword(),
'userTypeId' => $this->user_type_id,
'description' => $user['Location'],
]);
if ($res->status !== 200) {
throw new Exception($res->body);
}
}
$this->logOrShowAction("Done.");
}
}
/**
* Update gsgate users data from database array
*
* #param array $users
*/
private function updateUsers($users)
{
if (!empty($users)) {
$this->logOrShowAction("Begin to update users data.");
foreach ($users as $user) {
$res = $this->api->updateUser([
'email' => $user['Email'],
'name' => $user['FirstName'],
'surname' => $user['LastName'],
'driverID' => $user['RFID'],
'description' => $user['Location'],
], $user['gpsgate_id']);
if ($res->status !== 200) {
throw new Exception($res->body);
}
}
$this->logOrShowAction("Done.");
}
}
/**
* Compare arrays and return list of users data to update/create
*/
private function compare($gpsgate_users, $db_users)
{
$this->logOrShowAction("Begin to compare users data.");
$gpsgate_user_key = 'username';
$db_user_key = 'UserName';
$gpsgate_users = Common::setIndexesByKey($gpsgate_user_key, $gpsgate_users);
$res = [
'update' => [],
'create' => [],
];
foreach ($db_users as $user) {
$user_key = $user[$db_user_key];
if (!empty($gpsgate_users[$user_key])) {
if ($this->userInfoVary($gpsgate_users[$user_key], $user)) {
$user['gpsgate_id'] = $gpsgate_users[$user_key]->id; // add gpsgate id
$res['update'][] = $user;
}
} else {
$res['create'][] = $user;
}
}
$this->logOrShowAction('Done');
$this->logOrShowAction('Need to create: ' . count($res['create']) . ' rows; to update: ' . count($res['update']) . ' rows.');
return $res;
}
/**
* Check is the information between db user and api user is different
*/
private function userInfoVary($gpsgate_user, $db_user)
{
return $db_user['Email'] != $gpsgate_user->email
|| $db_user['FirstName'] != $gpsgate_user->name
|| $db_user['LastName'] != $gpsgate_user->surname
|| $db_user['RFID'] != ($gpsgate_user->driverID ?? '')
|| $db_user['Location'] != $gpsgate_user->description;
}
/**
* Connects to database, REST API, creates folders for errors and actions if need
*/
private function prepare()
{
if ($this->errors_report_type == 'log') {
if (!file_exists(dirname(Config::API_LOG_FILE_ERRORS))) {
mkdir(dirname(Config::API_LOG_FILE_ERRORS));
}
if (!file_exists(Config::API_LOG_FILE_ERRORS)) {
file_put_contents(Config::API_LOG_FILE_ERRORS, '');
}
}
if ($this->actions_report_type == 'log') {
if (!file_exists(dirname(Config::API_LOG_FILE_ACTIONS))) {
mkdir(dirname(Config::API_LOG_FILE_ACTIONS));
}
if (!file_exists(Config::API_LOG_FILE_ACTIONS)) {
file_put_contents(Config::API_LOG_FILE_ACTIONS, '');
}
}
$this->logOrShowAction('Trying to connect to database and GPSGATE REST API.');
$this->api = new Gpsgate_API(Config::API_APPLIATION_ID, Config::API_USER_TYPE_ID);
$this->db = DB::instance();
$this->logOrShowAction('Done.');
}
/**
* Logs error message in file or output in browser
*
* #param string $msg
*/
public function logOrShowError($msg)
{
$msg = "<span style='color: red; font-weight: 600;'>Error: " . $msg . "</span>";
$this->writeOrEchoMessage($msg, Config::API_LOG_FILE_ERRORS, $this->errors_report_type);
}
/**
* Logs action message in file or output in browser
*
* #param string $msg
*/
public function logOrShowAction($msg)
{
$this->writeOrEchoMessage($msg, Config::API_LOG_FILE_ACTIONS, $this->actions_report_type);
}
private function writeOrEchoMessage($msg, $file, $report_type)
{
if ($report_type == 'none') {
return ;
}
$msg = '[' . date('Y-m-d H:i:s') . '] ' . $msg;
if ($report_type == 'show') {
echo $msg . '<br>';
} else {
$h = fopen($file, 'a+');
fwrite($h, strip_tags($msg) . PHP_EOL);
fclose($h);
}
}
}
$sync = new API_Sync();
try {
$sync->run();
} catch (\Exception $e) {
$sync->logOrShowError($e->getMessage());
}

How to edit and remove image from post (CRUD Symfony 4)

I've started a new project, creating Entities, Controller, CRUD.
I added field to upload images, when I create a new one, everything works fine but I'm struggling with edit and delete.
==============
/**
* #Route("/{id}/edit", name="post_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Post $post): Response
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** #var UploadedFile $imageFile */
$imageFile = $form->get('image')->getData();
if ($imageFile) {
$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$imageFile->guessExtension();
// Move the file to the directory
try {
$imageFile->move(
$this->getParameter('images_directory'),
$newFilename
);
} catch (FileException $e) {
echo 'Impossible d\'enregistrer l\'image';
}
$post->setImage($newFilename);
}
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('post_index');
}
return $this->render('post/edit.html.twig', [
'post' => $post,
'form' => $form->createView(),
]);
}
/**
* #Route("/{id}", name="post_delete", methods={"POST"})
*/
public function delete(Request $request, Post $post): Response
{
if ($this->isCsrfTokenValid('delete'.$post->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($post);
$entityManager->flush();
}
return $this->redirectToRoute('post_index');
}
==============
I would like to know how to remove/edit image file from images_directory.
==============
EDIT :
I've found this solution :
Post_edit:
/**
* #Route("/{id}/edit", name="post_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Post $post, LoggerInterface $logger): Response
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** #var UploadedFile $imageFile */
$imageFile = $form->get('image')->getData();
$imageFileName = $post->getImage();
if ($imageFile) {
$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$imageFile->guessExtension();
// Move the file to the directory
try {
$imageFile->move(
$this->getParameter('images_directory'),
$newFilename
);
} catch (FileException $e) {
echo 'Impossible d\'enregistrer l\'image';
}
$pathToFile = $this->getParameter('images_directory').'/'.$imageFileName;
if (file_exists($pathToFile)) {
$logger->error("Le fichier $pathToFile existe.");
unlink($pathToFile);
} else {
$logger->error("Le fichier $pathToFile n'existe pas.");
}
$post->setImage($newFilename);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($post);
$entityManager->flush();
return $this->redirectToRoute('post_index');
}
return $this->render('post/edit.html.twig', [
'post' => $post,
'form' => $form->createView(),
]);
}
==============
Post_delete :
/**
* #Route("/{id}", name="post_delete", methods={"POST"})
*/
public function delete(Request $request, Post $post, LoggerInterface $logger): Response
{
if ($this->isCsrfTokenValid('delete'.$post->getId(), $request->request->get('_token'))) {
$imageFileName = $post->getImage();
$pathToFile = $this->getParameter('images_directory').'/'.$imageFileName;
if (file_exists($pathToFile)) {
$logger->error("Le fichier $pathToFile existe.");
unlink($pathToFile);
} else {
$logger->error("Le fichier $pathToFile n'existe pas.");
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($post);
$entityManager->flush();
}
return $this->redirectToRoute('post_index');
}
CAUTION: you are storing user data as directory/file, this is dangerous and strongly discouraged, because the user can alter the POST data and overwrite/delete/read stuff from other users and your filesystem. This is a BIG security exploit. You should determine the filepath yourself, not let the user browse through your directories.
That being said, if you still want to proceed using this approach:
You are not storing the filepath, so you can never know where the file was stored. Store the filepath together with the filename in your post, like this:
//...
// Move the file to the directory
try {
$pathToFile = $this->getParameter('images_directory').'/'.newFilename;
$imageFile->move(
$this->getParameter('images_directory'),
$newFilename
);
} catch (FileException $e) {
echo 'Impossible d\'enregistrer l\'image';
}
$post->setImage($pathToFile);
//...

validating base64 image laravel

Im saving a request to my database from my vue js via;
public function store(Request $request)
{
//validate
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'price' => 'required'
]);
//get image
$exploded = explode(',', $request->cover_image);
$decoded = base64_decode($exploded[1]);
if(str_contains($exploded[0],'jpeg'))
$extension = 'jpg';
else
$extension = 'png';
$fileName = str_random().'.'.$extension;
$path = public_path().'/cover_images/'.$fileName;
file_put_contents($path, $decoded);
//save
$product = new Product;
$product->name = $request->input('name');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->cover_image = $fileName;
if($product->save()) {
return new ProductsResource($product);
}
}
How can I validate the base64 image? Is my procedure in saving the image coming from vue js is in correct way or is there a better way? please let me know. Thanks im just new to laravel and vue js hoping to learn more
You should add this function to your custom helper :
if (!function_exists('validate_base64')) {
/**
* Validate a base64 content.
*
* #param string $base64data
* #param array $allowedMime example ['png', 'jpg', 'jpeg']
* #return bool
*/
function validate_base64($base64data, array $allowedMime)
{
// strip out data uri scheme information (see RFC 2397)
if (strpos($base64data, ';base64') !== false) {
list(, $base64data) = explode(';', $base64data);
list(, $base64data) = explode(',', $base64data);
}
// strict mode filters for non-base64 alphabet characters
if (base64_decode($base64data, true) === false) {
return false;
}
// decoding and then reeconding should not change the data
if (base64_encode(base64_decode($base64data)) !== $base64data) {
return false;
}
$binaryData = base64_decode($base64data);
// temporarily store the decoded data on the filesystem to be able to pass it to the fileAdder
$tmpFile = tempnam(sys_get_temp_dir(), 'medialibrary');
file_put_contents($tmpFile, $binaryData);
// guard Against Invalid MimeType
$allowedMime = array_flatten($allowedMime);
// no allowedMimeTypes, then any type would be ok
if (empty($allowedMime)) {
return true;
}
// Check the MimeTypes
$validation = Illuminate\Support\Facades\Validator::make(
['file' => new Illuminate\Http\File($tmpFile)],
['file' => 'mimes:' . implode(',', $allowedMime)]
);
return !$validation->fails();
}
}
Then extend the base64_image validation in your AppServiceProvider in boot() method :
use Illuminate\Support\Facades\Validator;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
...
Validator::extend('base64_image', function ($attribute, $value, $parameters, $validator) {
return validate_base64($value, ['png', 'jpg', 'jpeg', 'gif']);
});
}
Now you can use it in your validation rules like this :
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'photo' => 'required|base64_image'
];
}
There is a crazybooot/base64-validation package that handles base64 validation.
For installation instructions and more details see:
https://github.com/crazybooot/base64-validation

How to create a file upload REST API controller for Yii2

I am using Ionic framework for mobile app development. The Yii2 API code below can be used for file upload, but it doesn't work. It shows the following errors:
i) Undefined offset: 0.
ii) yii\db\BaseActiveRecord->save()
public function actionNew() {
$model = new Apiprofile();
$userid = $_REQUEST['user_id'];
$photo = $_FILES['photo'];
$model->user_id = $userid;
$model->photo = $photo;
$name = $model->user_id;
$model->file = UploadedFile::getInstance($model, 'photo');
if($model->file) {
$model->file->saveAs('uploads/photos/'.$name.'.'.$model->file->extension);
$model->photo = $name.'.'.$model->file->extension;
$model->save();
}
$name = $model->user_id;
if($model->save()) {
echo json_encode(array('status'=>1,'data'=>$model->attributes),JSON_PRETTY_PRINT);
} else {
echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
}
}
Hi if you want I will share my helper class that I used for working with images in Yii2 REST.
In the base folder of your application create folder components and inside of that folder create two folders helpers and objects.
-->assets
-->componests
----->helpers
----->objects
-->config
-->...
after that create class FileUpload inside of objects folder and put this code inside.
<?php
namespace app\components\objects;
class FileUpload
{
public $error=[];
public $isSuccess=false;
public $file;
public $ready_path;
public $file_type;
public $file_size;
public $file_extension;
public $file_tmp_name;
public $file_name;
public $save_path;
public function __construct($file,$save_path,$ready_path,$required=false)
{
if(!isset($_FILES[$file])){
if($required){
$this->error=['message'=>$file.' is required'];
}
return $this;
}
$this->save_path=$save_path;
$this->ready_path=$ready_path;
$this->file_type = strtolower($_FILES[$file]['type']);
$this->file_name = $_FILES[$file]['name'];
$this->file_tmp_name=$_FILES[$file]['tmp_name'];
$this->file_size = $_FILES[$file]['size'];
$this->file_extension=pathinfo($this->file_name, PATHINFO_EXTENSION);
}
public function setError($error){
if(empty($this->error)){
$this->error=$error;
}
}
}
Then inside of helpers folder create class FileUploadHelper and then put this code inside.
<?php
namespace app\components\helpers;
use app\components\objects\FileUpload;
use Yii;
class FileUploadHelper
{
private $allowed_files;
private $file_size;
const IMAGE='image';
const FILE='file';
/** File Upload
* #param string $file_name
* #param string $save_path
* #param string $ready_path
* #param string $type
* #param bool $required
* #return \app\components\objects\FileUpload
*/
public static function fileUpload($file_name,$save_path,$ready_path,$type,$required=false){
$image=new FileUpload($file_name,$save_path,$ready_path,$required);
if($type==self::FILE){
$allowed_files=Yii::$app->params['allowed_files'];
$file_size=Yii::$app->params['file_max_size'];
}else{
$allowed_files=Yii::$app->params['allowed_files'];
$file_size=Yii::$app->params['file_max_size'];
}
$dir = realpath(Yii::$app->basePath);
if(in_array($image->file_type,$allowed_files)
&&$image->file_size<$file_size){
$filename = $file_name.'_'.md5(uniqid(time()).time() . '_' . date('YmdHis')) . '.' . $image->file_extension;
$file = $dir . $image->save_path . $filename;
if(move_uploaded_file($image->file_tmp_name, $file)){
$image->file=$image->ready_path. $filename;
$image->isSuccess=true;
$image->setError(['message'=>'file_upload_success']);
}else{
$image->setError(['message'=>'error_try_again']);
}
}else{
$image->setError(['message'=>'file_should_be_no_more_than_given_size']);
}
return $image;
}
/** Delete File
* #param string $ready_file
*/
public static function deleteImage($ready_file){
$dir = realpath(Yii::$app->basePath);
if (strpos($ready_file, 'default') === false){
if(is_file($dir.'/web/'.$ready_file)){
unlink($dir.'/web/'.$ready_file);
}
}
}
}
That's all needed. Below I will give you example
public function actionEditAvatar($id)
{
$product = Product::findOne($id);
if( $product ){
$old_avatar=$product->avatar;
$image=FileUploadHelper::fileUpload(ProductForm::AVATAR,Yii::$app->params['product_path'],Yii::$app->params['product_ready_path'],FileUploadHelper::IMAGE);
if($image->isSuccess) {
$product->avatar = $image->file;
if($product->save()){
FileUploadHelper::deleteImage($old_avatar);
return $product->avatar;
}
}
return $image->error;
}
throw new NotFoundHttpException;
}
The code above from a real project. FileUploadHelper has two static classes which are "fileUpload" and "deleteImage".
FileUploadHelper requires fileUpload('file_name','save_path','ready_path','type')
'save_path' is where the file will be saved.
'ready_path' is how the ready URL should be like.
they are in Yii::$app->params[];
You can check if image succeeded or not by attribute isSuccess. If you have the error you can access them by attribute error. The ready file can be accessed by attribute file. You can delete image via static function deleteImage('saved_image_url'); All of them are used at above action. Please see it.
By the way, here the params that I used. Do not forget to to create folders inside web/uploads and change names of the folders like in config file.
return [
'adminEmail' => 'admin#example.com',
'allowed_images' => [ 'image/jpeg', 'image/gif', 'image/png' ],
'allowed_files' => [ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword', 'application/pdf','image/jpeg', 'image/gif', 'image/png'],
'image_max_size' => 2097152,
'file_max_size' => 8388608,
'owner_document_path' => '/web/uploads/owner_document/',
'owner_document_ready_path' => 'uploads/owner_document/',
'product_path' => '/web/uploads/product/',
'product_ready_path' => 'uploads/product/',
'complain_photo_path' => '/web/uploads/complain_photo/',
'complain_photo_ready_path' => 'uploads/complain_photo/',
'owner_path' => '/web/uploads/owner/',
'owner_ready_path' => 'uploads/owner/',
'staff_path' => '/web/uploads/staff/',
'staff_ready_path' => 'uploads/staff/',
];

Simple update a text content in Google Cloud Storage

I'm a new user of Google Cloud Storage, so bear with me.
I'm trying to create a file editor, that gets a non-binary file from GCS, and saves it back.
I'm using google-api-php-client. I've been experimenting a lot using the API, browsed, but I just couldn't find the proper answer.
<?php
class GCS_Driver {
/** #var Google_Service_Storage $driver */
public $driver;
/** #var string $bucket */
public $bucket;
public function updateObject($objectPath,$content) {
$updated = false;
try {
$postBody = new Google_Service_Storage_StorageObject();
$updated = $this->driver->objects->patch($this->bucket,$objectPath,$postBody,array(
'data' => $content // I know this is wrong, I am just showing the idea I am looking for to overwrite the content
));
} catch (Exception $ex) {
// error log
return false;
}
if (!$updated) {
// error log
return false;
}
return true;
}
}
Any hint will be appreciated.
I figured out the work around.
Basically, just save a temp file, and upload it to overwrite the existing
Here's the code.
<?php
class GCS_Driver {
/** #var Google_Client $client */
public $client;
/** #var Google_Service_Storage $driver */
public $driver;
/** #var string $bucket */
public $bucket;
public function updateObject($objectPath,$content) {
$updated = false;
try {
$temp = tempnam(sys_get_temp_dir(), 'gcs');
$handle = fopen($temp, "r+b");
fwrite($handle, $content);
fseek($handle, 0);
$postBody = new Google_Service_Storage_StorageObject();
$postBody->setName($objectPath);
$postBody->setUpdated(date("c"));
$postBody->setGeneration(time());
$size = #filesize($temp);
$postBody->setSize($size);
$ext = #pathinfo($objectPath,PATHINFO_EXTENSION);
$ext = strtolower($ext);
$mimeType = $this->getContentType($ext);
$postBody->setContentType($mimeType);
$chunkSizeBytes = 1 * 1024 * 1024;
$this->client->setDefer(true);
$request = $this->driver->objects->insert($this->bucket,$postBody,array(
'name' => $objectPath,
'predefinedAcl' => 'publicRead',
'projection' => 'full',
));
$media = new Google_Http_MediaFileUpload(
$this->client,
$request,
$mimeType,
null,
true,
$chunkSizeBytes
);
$media->setFileSize($size);
$status = false;
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
unlink($temp);
if ($status) {
$updated = true;
}
} catch (Exception $ex) {
// error log
return false;
}
if (!$updated) {
// error log
return false;
}
// action log
return true;
}
}
Any new feedback is welcome.

Categories