What is the most fast way to show "secure" images with Laravel and Amazon s3? - laravel

In my Laravel application I have a gallery for logged-in users with Amazon S3.
Now I download EVERY thumbnail and image in this way:
public function download($file_url){ // is safe, but slow...
$mime = \Storage::disk('s3')->getDriver()->getMimetype($file_url);
$size = \Storage::disk('s3')->getDriver()->getSize($file_url);
$response = [
'Content-Type' => $mime,
'Content-Length' => $size,
'Content-Description' => 'File Transfer',
'Content-Disposition' => "attachment; filename={$file_name}",
'Content-Transfer-Encoding' => 'binary',
];
return \Response::make(\Storage::disk('s3')->get($file_url), 200, $response);
}
This is safe (because I have a router with middleware('auth'), but is very server-intensive and slow.
Is it possible to download a file directly from Amazon:
only for (in my Laravel)-loggedin users (mayby with a temporery download link)?
OR only with a secure unique link?

You can use temporary URLs:
$url = Storage::disk('s3')->temporaryUrl(
'file.jpg', now()->addMinutes(5)
);
First param is the path on S3, second param is how long you want the URL to work for. Set this to a low value if you only want the URL to work for a single page load.
https://laravel.com/docs/5.6/filesystem (under Temporary URLs)

Related

Sales force refresh token could not generating in Laravel php

hope you all are doing well. I am stuck with an issue since couple of hours and could not be able to resolve it. Please have a look on error
enter image description here
I am trying to generate refresh token. Access_token generated successfully that is expired after 20 minutes. refresh token could not be generated
$client = new Client();
$res = $client
->request("POST",
"https://test.salesforce.com/services/oauth2/token",[
'body'=> json_encode([
'grant_type'=>'refresh_token',
'client_id'=>Config::get('forrest.info.client_id'),
'client_secret'=>Config::get('forrest.info.client_secret'),
'Authorization'=>'Bearer 00D3H0000008olE!ARYAQOzGGJDDIaygn7GokNk4T319XlDwwBXIDYPkJfej74dfhFT3CMDvBD2pZBQK_x.v2amiRe5Y29ZNXMp9EOA6ZTYyqrTW',
'access_token'=>'00D3H0000008olE!ARYAQOzGGJDDIaygn7GokNk4T319XlDwwBXIDYPkJfej74dfhFT3CMDvBD2pZBQK_x.v2amiRe5Y29ZNXMp9EOA6ZTYyqrTW',
])
]
);
dd ($res->getStatusCode());
any help or comment would be highly appreciated! thank for your time.
It seems like this error is returned from SalesForce API.
Looking at their documentation, it seems that they expect to see application/x-www-form-urlencoded not json.
Try out this code:
$client = new Client();
$response = $client->request('POST', '/post', [
'form_params' => [
'grant_type' => 'refresh_token',
'client_id' => Config::get('forrest.info.client_id'),
'client_secret' => Config::get('forrest.info.client_secret'),
'Authorization' => 'Bearer 00D3H0000008olE!ARYAQOzGGJDDIaygn7GokNk4T319XlDwwBXIDYPkJfej74dfhFT3CMDvBD2pZBQK_x.v2amiRe5Y29ZNXMp9EOA6ZTYyqrTW',
'access_token' => '00D3H0000008olE!ARYAQOzGGJDDIaygn7GokNk4T319XlDwwBXIDYPkJfej74dfhFT3CMDvBD2pZBQK_x.v2amiRe5Y29ZNXMp9EOA6ZTYyqrTW',
],
]);
dd($response);
let me know if it helped.
(Not an answer but too long for a comment)
This looks weird. "Authorization Bearer" should be a http header, not part of the message's body. Are you able to do what you need in plain old Postman, SoapUI etc?
Which OAuth2 flow did you use to generate this access token? Not all flows generate refresh token, for example https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_username_password_flow.htm&type=5 says "this flow doesn't support scopes" so you get full access - but also "this response doesn’t send a refresh token."
Have you checked the connected app (place in SF where you took client id and secret from), do the OAuth2 scopes include refresh? Did you ask for refresh token (included it in the original request's grants)?

Laravel: Output array of bytes to browser in pdf format

I am getting a response from an external api which contains an array of bytes I'd like to convert to pdf.
Sample response from the api.
So far, I have tried below code in laravel with no success.
$data is received from api.
return response($data)
->withHeaders([
'Content-Type'=> 'application/pdf'
]);
The trick is to set the Content-Disposition header to force a download.
return response($data, 200, [
'Content-type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="api.pdf"',
]);
You could also replace attachment with inline to render the pdf in the browser (most modern browser support this).

Downloading zip file results in .cpgz file after extraction

I am using the following code do download a zip file. I am sure the file is existing and working on the server. The result is .cpgz file after extraction.
return response()->download('/Applications/XAMPP/xamppfiles/htdocs/stoproject/source/storage/app/Copy of StoTherm Classic®.zip');
The code was working and without any change, it stopped.
I also tried adding headers:
$headers = array(
'Content-Type' => 'application/zip',
);
return response()->download('/Applications/XAMPP/xamppfiles/htdocs/stoproject/source/storage/app/Copy of StoTherm Classic®.zip', basename('/Applications/XAMPP/xamppfiles/htdocs/stoproject/source/storage/app/Copy of StoTherm Classic®.zip'), $headers);
Also tried with:
'Content-Type' => 'application/octet-stream'
Calling ob_end_clean() fixed the issue
$response = response()->download($pathToFile)->deleteFileAfterSend(true);
ob_end_clean();
return $response;
Laravel 4 Response download issue

Get the incoming message on my clickatell number through http api

I want when someone message me on my clickatell number then I want to send this message on my email. I'm using the http request.I have check the http api pdf in which only showing the authentication and send message url . I want the url by whcih I get the message in my script.
I'm using the curl function for request the page.
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://api.clickatell.com/http/auth?user=demo&
password=demo&api_id=123432',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => 'value',
item2 => 'value2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
print_r($resp);
// Close request to clear up some resources
curl_close($curl);
It is working and return me the session id and ok status. similarly can I get the incoming message ?
Clickatell has a two-way service which will allow you to receive messages from their service and their implementation documentation can be read here.
In order to receive the notice via email, I would recommend opting for the HTTP callback from Clickatell's servers. This will allow you to write a small script to convert the contents into an email.
Hope that this helps you.

Calling XMLHTTPRequest from outside javascript

I need to parse the content resulting from a XMLHttpRequest, not from the browser but using PHP/or PERL or whatever, BUT javascript.So I tried to make a POST based on my understanding of the specific request I need to parse but I get in return a 404 (page not found).
Where is my mistake?
1) Incorrect post message
2) The server understands that my request is not coming from XMLHttprequest and then rejects with a 404.
3) Other I do not know.
Sorry, I cannot post the URL because it is inside the Intranet.
I will post the code written in perl:
use CGI;
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $response
= $ua->post( $myUrl,
{ bId => '5',
'c0id' => '0',
'c0mN' => 'cM',
'c0p0' => 'string3.57',
'c0p1' => 'boolean:false',
'c0sN' => 'mD',
'cC' => '1',
'httpSessionId' => '0C23946319623A',
'page' => $relPage,
'scriptSessionId' => '67233543259'
})
You don't have to do anything special with those other methods; just set the request type in the open() call, and send() an appropriate body (the item you're putting in the case of "PUT"; a null argument in the case of "GET").
Do you have a timeout mechanism and event scheduling in place?

Resources