How can I save an image via API in Laravel Server from React Native - laravel

I am trying to save some info and an image sent through api from React Native app. This is the request that I get if I log it.
[2022-08-05 10:11:09] local.ERROR: array ('{
"data":{
"_parts":' => array ('[
"image", {
"modificationDate":"1659694264000","size":2883040,"mime":"image/jpeg","height":4160,"width":3120,"path":"file:///storage/emulated/0/Android/data/com.wheeloffortune/files/Pictures/image-6ab1ad33-2580-416b-be7d-09a0662739218182247041115654861.jpg"
}' => NULL,
),)
When I tested api from postman I get image as a file and have saved it easily without a sweat. But from the app the developer sends in the format I pasted above. Can I somehow save image from the above request.

Related

Unable to get content of file using google drive api

I am working with a react application where you can access google drive files using react-google-picker and I am using get api of google apis where you can pass "alt=media" as a URL parameter to get the file content. It's working as expected for text files but for PDF/Doc files, it returns metadata instead of actual content of file.
Is there any other way to get the content using google api's ??
this is the code for getting content of the file.
axios.get(`https://www.googleapis.com/drive/v2/files/${data.docs[0].id}?key=${developerKey}&alt=media`, {
headers: {
Authorization: Bearer ${this.state.token}
}
})

Laravel download from google drive (filesystem) or (hide api key from link)

First way to download:
https://www.googleapis.com/drive/v3/files/1YIH4zfM0P1xa-_mfZNGiIY8qZrIEt-rF/?key=API_KEY&alt=media
Putting my API KEY in the link it gives me ability to download the file directly from my google drive.But I don't want to give my API_KEY to the users.
2.I can also access my drive another way: (using nao-pon/flysystem-google-drive)
Route::get('/download/{rest?}', function ($rest) {
$metadata = Storage::cloud()->getMetadata($rest);
$readStream = Storage::cloud()->getDriver()->readStream($rest);
return response()->stream(function () use ($readStream) {
fpassthru($readStream);
}, 200, [
'Content-Type' => $metadata['mimetype'],
'Content-disposition' => 'attachment; filename="'.$metadata['name'].'"', // force download?
]);
})->where('rest','(.*)');
This way I don't have to use api_key but the server has to download the whole file it's a stream but still that uses server resources.
On the other hand https://www.googleapis.com/drive/v3/files/1YIH4zfM0P1xa-_mfZNGiIY8qZrIEt-rF/?key=API_KEY&alt=media needs Content-Type and File name as it doesn't have on it.And also I have no idea how to hide the api key.
So what do you suggest.Is there any other way not to response()->stream to not download the whole file by stream through server and then send it to the user?Multiple users downloading the files would use all the bandwidth,so the download speed drops so fast.

Is it possible to use a file path instead of url as `image_url` when sending a message via Slack API or Incoming Webhook?

Let's assume I have the following block which I want to send via Incoming Webhook to Slack
{
"type": "image",
"title": {
"type": "plain_text",
"text": "foo bar"
},
"image_url": "https://api.slack.com/img/blocks/example/beagle.png",
"alt_text": "foo"
}
but instead of providing a http url as image_url I would like to provide a file path because the file I want to send is in the same folder as my script.
1) Is this possible? I guess no.
2) Is it possible to upload an image via files.upload in a first step WITHOUT actually displaying is on any channel just to get the URL which then can be used as image_url?
1) No. As I mentioned earlier its not possible to use a file path to directly include an image in a message. The only way is to provide a public URL to an image file.
2) Yes, you can use files.upload you uploaded to Slack in your messages. And you do not have to share them in any channel do it.
The basic approach is:
Upload your image to Slack with files.upload. And if you do not provide any channels with that API call the file will not be shared in any channel. You will get the file object incl. the file ID of the uploaded image file.
Call files.sharedPublicURL to make that file public by providing the file ID
Your image file is now public and you can use the permalink_public of the file object to include that image in message. However, you still need to construct the direct URL for that image from the URL you get from permalink_public. Check out this post on how to get the direct URL for the image.

laravel XMLHttpRequest cannot load https://website.com/images/1554690945.png. No 'Access-Control-Allow-Origin' header is present

I'm having problem with my laravel file system CORS, I'm trying to cache the image from the url (which is also my website) in my ionic application but it's failing because of the error. I tried the image from https://reqres.in/api/users/1 and there is no problem caching the image in my ionic application. I guess the problem here is in my laravel website
In one of my current projects I have to save 200+ images in my Ionic App from a request to my server.
The way I handled this problem was converting the image to Base64 using Image Intervention and responding to the request with back to the app to then save the Base64 in the Ionic Storage like so.
Laravel Controller
public function grabImages(Request $request){
$image = (string) Image::make('public/bar.png')->encode('data-url');
$data = {
'base64' : $image,
'file_name' : 'test'
}
return $data;
}
Ionic
After receiving the data you can just store it in the Ionic Storage and access it wherever you would like to, even offline.
To display it all you have to do is set the image source to the Base64.
Using this method also solves a few problems, such as the user cannot see the images in the image gallery, as well as allows you to store them and use them offline for as long as you would like and remove them whenever.
As ImJT said I am using the barryvdh's laravel-cors plugin as well.
Hope this answered your question, good luck!

How to get Uploaded Url of a Video File in android using Parse REST API

I am using Parse Mobile Backend for my android app,but whenever I want to upload a large video more than the default 10mb limit for a ParseFileby getting the bytes from it into a ParseFile I keep running into the dreaded OutOfMemory Exception .So,I want to use the Parse REST API since I can easily use the setChunkedStreamingMode(1024) in HttpUrlConnectionto send the bytes in a chunked manner.The trouble is,how do I get the uploaded url of the file uploaded.Thanks for your help in advance.
i think you can use getUrl() method
ParseFile file = new ParseFile(byte[] data);
file.getUrl();
ParseFile object has getUrl() method to getting url of file. this method return URL in string format.

Resources