This episode is for members only

Sign up to access "Build a Livewire CSV Importer/Exporter" right now.

Get started
Already a member? Sign in to continue
Playing
22. Expiring downloads

Transcript

00:00
Okay, let's look at a slightly different way to handle the download of an export.
00:05
Now really this depends on what you're building, how you're building it, what security you want between authenticated users. Let's just review what we're doing at the moment. So if we head over to the browser, when I go ahead and click on download, that is just downloading the file itself. Now these files we know, let's just talk about storage, we know that these are stored over here in this private directory.
00:28
Now this cannot be accessed as a public URL. So for example, we can't do something like livewire-csv.test 1 and then the name of the file, it just doesn't get found, it's basically hidden away. Now the alternative way to do this would be to generate a link to this. Now this would be useful if you did want links to be shared between other people, perhaps people that are not signed in or don't have permission to access this.
00:59
And here's how you would do it. Now what we're going to do is jump straight in with temporary URLs because we want to make sure that these URLs get generated so we can publicly access them. But we do not want them to be available for more than we need, more time than we need. So when we use temporary URL, we give the path to it that we want to generate and then we give an expiry.
01:24
So to do this really easily, we can grab the current date and time and we can add on a certain amount of time. So just to test this out, let's add on say 30 seconds. So let's say add seconds and 30 and this isn't going to work on its own, so just returning this isn't going to work. We can still click it, but what that's going to do is it's going to actually generate a URL to the file.
01:50
Now let's just take a look at the structure of what we've got here. So I'm going to go ahead and take all of this up to the query string. So the path to the CSV, which remember I said we can't access and we're going to go ahead and just try and hit this in the browser and you see we get a 403 forbidden. So it's there, but we can't access it unless we've generated a temporary URL with a signature and an expiry.
02:15
So the signature will contain the expiry, which will be validated. So this can't be tweaked. Let's go ahead and hit that. And again, it's still forbidden, but that's just because I've been speaking for more than 30 seconds. Let's give that a refresh to refresh this and let's go ahead and try and download that and you can see that it works.
02:34
So this is great if you do want these URLs to be shareable, but you want the expiry and signature in here. So what do we do here then? Because at the moment when I go ahead and click on download, it just goes ahead and shows this. Well, what we can do is we can redirect to this or you can implement some ability to copy and paste this URL or you could show the URL, really whatever you want to do. I'm just kind of giving you options here.
03:01
So we're going to say redirect and we're just going to wrap that. And there we go. So we need to do. So let's go back over, click download. And there we go. We get redirected to that. So this is, again, useful if you want to show the URL anywhere. You now have a couple of options that you can choose from.
22 episodes2 hrs 18 mins

Overview

Let's build a powerful CSV importer and exporter with Livewire, completely from scratch.

This can handle millions of rows, be reused for multiple models, and by using queues, doesn't require the browser to be open.

This course is for you if:

  • You need a robust drop-in importer and exporter component for your models that you have full control over
  • You want to brush up on some advanced Livewire concepts
  • You want to learn about queues in Laravel
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!