I am working on a system that upload a file into firsbase and after uploading get its downlaod link with any expire at timeing.
Here is my code ( In my code I will uploading the file and after that when I click on a button then Its give me a download link with some expire time.
Code : (uploading )
public function upload_file(Request $request){
$rules = array(
'title' => 'required',
'tags' => 'required',
'cover' => 'required',
'category' => 'required',
'file_to_be_upload' => 'required',
'datepicker' => 'required'
);
$messages = [
'doc_file.required' => 'The Upload File field is required',
'datepicker.required' => 'The Select Date field is required'
];
$validator = Validator::make($request->all(),$rules, $messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}else {
$title = $request->input('title');
$tags = $request->input('tags');
$file_to_be_upload = $request->file('file_to_be_upload');
$type = $request->input('type');
$fileName = $file_to_be_upload->getClientOriginalName();
$extension = $file_to_be_upload->getClientOriginalExtension();
// // Date String
$expl = explode('-', $request->input('datepicker'));
$current_date = $expl[0];
$current_month = $expl[1];
$current_year = $expl[2];
$product_id = strtoupper($current_month) . 'DF' . $current_year . 'DF' . time();
$cover_url = $this->add_cover($request->file('cover'), $product_id);
// Storing Process
$student = app('firebase.firestore')->database()->collection($type)->document($fileName);
$firebase_storage_path = $type . '/' . $current_year . '/' . $current_month . '/' . $current_date . '/';
$name = $student->id();
$localfolder = public_path('firebase-temp-uploads') .'/';
$file = $name. '.' . $extension;
if ($file_to_be_upload->move($localfolder, $file)) {
$uploadedfile = fopen($localfolder.$file, 'r');
app('firebase.storage')->getBucket()->upload($uploadedfile, ['name' => $firebase_storage_path . $name]);
// will remove from local laravel folder
unlink($localfolder . $file);
// Storing All Details into Database
$result = Product::create([
'product_id' => $product_id,
'title' => $request->input('title'),
'slug' => $request->input('title'),
'description' => $request->input('description'),
'tags' => $request->input('tags'),
'type' => $request->input('type'),
'category' => $request->input('category'),
'doc_file' => $fileName,
'cover_image' => $cover_url,
'file_location' => $firebase_storage_path,
'status' => '0',
'added_by' => Cookie::get('admin_id'),
'uploaded_at' => $request->input('datepicker'),
])->categories()->sync($request->input('category'));
if($result){
return redirect()->back()->with( [ 'status' => "Document Uploaded Successfully" ] );
}else{
return redirect()->back()->with( [ 'status' => "Something went wrong" ] );
}
} else {
return 'error';
}
}
}
Code : (Getting Downlaod link)
$document = Product::where("product_id", $product_id)->first();
$file_location = $document->file_location;
$fileName = $document->doc_file;
$type = $document->type;
$expiresAt = new \DateTime('3600');
$imageReference = app('firebase.storage')->getBucket()->object($file_location . $fileName);
if($imageReference->exists()){
$image = $imageReference->signedUrl($expiresAt);
return redirect()->back()->with( [ 'link' => $image , 'product_id' => $product_id] );
}else{
return redirect()->back()->with( [ 'status' => "Something went wrong" ] );
}
I just wanted to do the same thing but In 1 time.
in above method I am requesting 2-3 times to get a single file.
Is there any way to upload the file and get the download URL (without expire at) and then I will save that url into database