don't you also need the folder name in the downloadAsset function?
I can see you are writing the file into the 'uploads' bucket, but can't see where you reference that anywhere when downloading?
I'm building a small asset management system within Laravel.
So far, so good but I'm having problems making my file available for download.
This method handles the upload
public function store(AssetRequest $request)
{
// Initialise new asset and set the name
// from the form
$asset = new Asset(array(
'name' => $request->get('name')
));
$asset->user_id = Auth::user()->id;
// save the asset to the db
$asset->save();
// set the file var = form input
$file = $request->file('asset_path');
$extension = $file->getClientOriginalExtension();
// modify the asset name
$assetFile = $asset->id . '.' . $request->file('asset_path')->getClientOriginalExtension();
// push the new asset to s3
Storage::disk('s3')->put('uploads/' . $assetFile, file_get_contents($file));
$asset->mime = $file->getClientMimeType();
$asset->original_filename = $file->getClientOriginalName();
$asset->filename = $assetFile;
$asset->file_extension = $extension;
// return ok
$asset->save();
return \Redirect::route('asset.create')->with('message', 'Asset added!');
}
and it works swell (can't help but feel it could be simpler).
For my view I have
<a href="/download/{{ $asset->id}}>download asset</a>
and the method
public function downloadAsset($id)
{
$fs = Storage::getDriver();
$stream = $fs->readStream($id);
return \Response::stream(function() use($stream) {
fpassthru($stream);
}, 200, [
"Content-Type" => $fs->getMimetype($path),
"Content-Length" => $fs->getSize($path),
"Content-disposition" => "attachment; filename=\"" .basename($path) . "\"",
]);
}
but I get
File not found at path: 57
Ideally I'd love to be able to store the S3 url in the db as well as the reference to the image and then just set download attribute on my button with {{ $asset->s3url }} as the href.
To clarify, my assets are stored in the db and S3, renamed to match the db.
Any ideas?
Got it licked with:
public function downloadAsset($id)
{
$asset = Asset::find($id);
$assetPath = Storage::disk('s3')->url($asset->filename);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=" . basename($assetPath));
header("Content-Type: " . $asset->mime);
return readfile($assetPath);
}
Please or to participate in this conversation.