possible to stream a mp3/mp4 from Dropbox API V2 with PHP? - laravel

Yesterday I set it up so I can serve MP3 files stored in my Dropbox using https://github.com/spatie/dropbox-api and Laravel. However this only works for small'ish files as the way it's working now, it has to load the entire file first and then serve it from Laravel. This doesn't work at all for movies or for long tracks as it takes forever and runs out of memory.
Here's the code I'm currently using
$authorizationToken = 'my-api-token';
$client = new \Spatie\Dropbox\Client($authorizationToken);
$path = "/offline/a-very-long-song.mp3"; // path in dropbox
$stream = $client->download($path);
$file = stream_get_contents($stream);
fclose($stream);
unset($stream);
$file_info = new \finfo(FILEINFO_MIME_TYPE);
return response($file, 200)->withHeaders([
'Content-Type' => $file_info->buffer($file),
'Content-Disposition' => 'inline; filename="' . basename($path) . '"',
]);
I was wondering if there's a way to stream it so it doesn't have to load the entire file first. I guess this happens naturally when you load a media file in the browser, but since there are no direct links to the physical file with Dropbox, I'm not sure if it's possible.

The Dropbox API does offer the ability to retrieve temporary direct links that can be used for streaming files like this, via the /2/files/get_temporary_link endpoint:
https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
In the library you're using, that appears to be available as the getTemporaryLink method, as shown in the example here:
https://github.com/spatie/dropbox-api#a-minimal-implementation-of-dropbox-api-v2

Related

I'm having troubles reading an excel file in laravel

I'm trying to read an excel file(xlsx), I defined the path using
$uri = "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx";
but I keep getting the error
File "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx"
does not exist. I'm new to this framework.
This is the controller code.
public function show($id)
{
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setReadDataOnly(TRUE);
$uri = "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx";
$spreadsheet = $reader->load($uri);
$worksheet = $spreadsheet->getActiveSheet();
return view('student.result', compact('worksheet'));
}
The path you're using is relative to the current directory that PHP is running in. For Laravel, that likely means you'll be running this in app/Services/MyUploadService.php or something similar.
Given your path is relative, PhpOffice will probably be trying to load `app/Services/public/storage... etc.' which is incorrect.
Laravel has helper methods that can assist you in working with paths within your app.
You could look at using base_dir() (so: base_dir('public/storage/example.xlsx').) as it will give you an absolute path, and a similar usage of public_path() would be better than that.
However, best out of all the options would be using storage_path(). So your code would instead read:
$reader->setReadDataOnly(TRUE);
$uri = storage_path("resultsheet/my.xlsx");
$spreadsheet = $reader->load($uri);
There's helpful documentation on the Path helpers in the Laravel docs.
To debug this, storing realpath('subdir/example.txt') etc. in a variable and using a debugging tool will help you figure out the relative/absolute paths in your project and server.

Generating Drupal image derivatives via a curl call

We have a content type that uses a number of image styles to re-purpose images for a variety of different sections of our website, and have a large number of derivatives that need to be generated.
I want to use a script to pre-generate the necessary image derivatives before we go live after a major upgrade.
My thought was to write a script that uses Curl to call the URLs for which image derivatives will be created.
If in a browser I go to a specific URL that will cause generation of a derivative the image gets generated as expected. This is default Drupal behavior.
However, if I make a call to Curl on the command line for another URL that will cause generation of a derivative, the image does not get generated as expected.
I suspect it's because Curl is not actually downloading images. I also tried with Lynx and the result was the same.
Can anyone advise if there is a way to force Curl or Lynx to automatically download images so that the derivatives get created?
Thanks,
Pablo
you want to download all <img src="url" /> 's ?
easy, parse out the src attributes with DOMDocument and make an individual curl request for each image, kinda like this:
function downloadAllImagesFromUrl(string $url):int{
$imagesDownloaded=0;
$ch=curl_init();
if(!curl_setopt_array($ch,array(
CURLOPT_AUTOREFERER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPGET => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CONNECTTIMEOUT => 4,
CURLOPT_TIMEOUT => 8,
CURLOPT_COOKIEFILE => "", // <<makes curl save/load cookies across requests..
CURLOPT_ENCODING => "", // << makes curl post all supported encodings, gzip/deflate/etc, makes transfers faster
CURLOPT_URL=>$url,
CURLOPT_RETURNTRANSFER=>true
))){
throw new Exception(curl_error($ch));
}
$html=curl_exec($ch);
$domd=#DOMDocument::loadHTML($html);
foreach($domd->getElementsByTagName("img") as $img){
$src=$img->getAttribute("src");
if(!$src){
continue;
}
//Warning: you might want to parse_url PHP_URL_HOST / PHP_URL_PORT / PHP_URL_PATH
// if the urls are not absolute but relative.
curl_setopt($ch,CURLOPT_URL,$src);
curl_exec($ch);
++$imagesDownloaded;
}
curl_close($ch);
return $imagesDownloaded;
}
It is probably much faster to use get_headers() instead of curl_exec, but since PHP by default use ignore_user_abort, drupal may abort image generation if you dont actually download them but only get their headers. warning, the code above assumes all image src's are absolute. you need additional coding with parse_url & PHP_URL_HOST / PHP_URL_PORT / PHP_URL_PATH if you want to handle relative urls.. and note: this can be made much faster by using multithreading with curl_multi interface, but that requires much more complex coding..

Media data upload and performance

I need to create RESTful API for uploading media data. I need to be able to handle hundreds (thousands) of simultaneous requests. Once data is uploaded to my server, we are going to store it on Amazon S3 and populate some meta data into database. Could you advice in a few questions:
1) Which language is better for these kind of tasks ? (I'm familiar with PHP and Perl)
2) What about server? (nginx ?)
3) We need to be able to scale easily in case there are a lot of requests
4) Anything else you could point out and advice ?
Thank you
use feature ":5.16";
use warnings FATAL => qw(all);
use strict;
use Data::Dump qw(dump);
use Amazon::S3;
my $s3 = Amazon::S3->new
({aws_access_key_id => "...",
aws_secret_access_key => "...",
retry => 1
}
);
my $b = $s3->bucket("Your bucket name");
my $f = "test.data";
$b->add_key_filename($f, "test.data",
{"x-amz-storage-class"=>"REDUCED_REDUNDANCY", 'x-amz-meta-version'=>'12.11.22', acl_short=>'public-read'
});
say dump($b->errstr) if $b->errstr;

Purpose of /var/resource_config.json

I'm trying to figure out what the purpose of the file /var/resource_config.json is in Magento. It appears to perhaps be a caching of a configuration, but can't see where in the source code it is being created and/or updated.
I'm in the process of setting up local/dev/staging/prod environments for an EE1.12 build and want to figure out if I can safely exclude it from my repo or whether I need to script some updates to it for deploys.
Maybe the flash image uploader in admin creates it?
Any ideas or directions to look?
This is a configuration cache file for the "alternative media store" system. This is a system where requests for media files are routed through get.php, and allows you to store media in the database instead of the file system. (That may be a gross over simplification, as I've never used the feature myself)
You can safely, (and should) exclude this file from deployments/source control, as it's a cache file and will be auto generated as needed. See the following codeblock in the root level get.php for more information.
if (!$mediaDirectory) {
$config = Mage_Core_Model_File_Storage::getScriptConfig();
$mediaDirectory = str_replace($bp . $ds, '', $config['media_directory']);
$allowedResources = array_merge($allowedResources, $config['allowed_resources']);
$relativeFilename = str_replace($mediaDirectory . '/', '', $pathInfo);
$fp = fopen($configCacheFile, 'w');
if (flock($fp, LOCK_EX | LOCK_NB)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($config));
}
flock($fp, LOCK_UN);
fclose($fp);
checkResource($relativeFilename, $allowedResources);
}
Speaking in general terms, Magento's var folder serves the same purpose as the *nix var folder
Variable files—files whose content is expected to continually change during normal operation of the system—such as logs, spool files, and temporary e-mail files. Sometimes a separate partition
and should be isolated to particular systems (i.e. not a part of deployments)

Need help transferring a jpg from web server to S3 - PHP and CodeIgniter

I have a Flash application that captures an image and passes an encoded image to my web server. The web server then decodes the image and saves it to a tmp directory. That part works fine. Next I want to move this image from the web sever to my S3 account but am having trouble. I am using the code below. Any help is appreciated.
In the CodeIgniter Controller Constructor:
$this->load->library('S3');
In the function (also within the controller for now)
/************** S3 upload example***************/
if (!defined('awsAccessKey')) define('awsAccessKey', 'xxxxxxx');
if (!defined('awsSecretKey')) define('awsSecretKey', 'xxxxxxx');
$s3 = new S3(awsAccessKey, awsSecretKey);
if($s3->putObjectFile($filePathTemp, "bucket", $filePathNew, "ACL_PUBLIC_READ_WRITE")){
#unlink($filePathTemp);
}
I can't even get the $s3 variable to return anything in an "echo" statement by returning a value on the first line of the S3 constructor. I can't access the S3 object/class, but the following statement returns a "1":
echo "S3 --> " . class_exists('S3');
Thanks in advance for your time.
-Tim
I'm assuming (you don't say: you should specify which one) you are using this. Your usage looks correct: I'm assuming your keys are wrong. If
print_r($s3->listBuckets());
doesn't return anything check your server logs.

Resources