jaseew01 / multi-user-file-manager

0 stars 0 forks source link

Document Download #14

Open jaseew01 opened 9 years ago

jaseew01 commented 9 years ago

Below are the notes I have from our last meeting. I have looked into each option, and just wanted to get your opinion. I guess I would just like to know which one you thought would be the most appropriate to implement.

-Content-Disposition header for sending filename over http? -Or set Content-Type header to multipart/form-data and use the boundary to relay the filname through other headers -Also Look at HTTP_Definitive_Guide to put random info into headers. -Another option would be to create file on harddrive, and send that file off -would return redirect to location of file -Look at expressjs middleware serve-static -express.static -to find this, go to express suite, guide, middleware

skiadas commented 9 years ago

I'm thinking these, which are essentially one way:

-Another option would be to create file on harddrive, and send that file off -would return redirect to location of file -Look at expressjs middleware serve-static -express.static -to find this, go to express suite, guide, middleware

jaseew01 commented 9 years ago

I looked at this, just to make sure that I am understanding it correctly, whenever I send a file off to the browser, express.static will simply populate certain header fields that get specified in the options argument correct?

jaseew01 commented 9 years ago

and it does this only for files served from the root folder.

skiadas commented 9 years ago

express.static sets things up so that when the browser asks for certain resources, they are accessed as files from a specific root folder on the disc, rather than you having to manually create requests. You should be able to tell it to monitor all requests that start with '/download'. So the first test that you have this working would be that you will have established a link between the '/download/...' requests and files/subfolders in a "download" folder, and if you put in the location bar a URL '.../download/afilename' it would serve as a response the file with name 'afilename' in the 'download' folder.

The next step would be to change your server's code, so that when the user asks for '/file/thefileid/download', a file with an appropriate name is created in the download folder, and you send back a redirect reply to a '/download/filename' address. The browser will then make a followup request to that address, which will be handled by express.static.

Once that is working, you need to set up a periodic method that will monitor the download folder, looking for "out-of-date" files and removing them.

jaseew01 commented 9 years ago

Okay, got it.

jaseew01 commented 9 years ago

does fs.writeFile(...) not create a file if it isn't already created?

skiadas commented 9 years ago

It should create it. But of course your code will not be able to see that until the callback, because it is asynchronous.

jaseew01 commented 9 years ago

My mistake, it does create the file, I was just creating it in the wrong spot. So now, I have the redirect inside of the writeFile(...) callback and the page is still getting redirected before the file actually exists.

skiadas commented 9 years ago

So in the callback, what happens if you try to look for the file directly? Commit what you have and point me to where I should look.

jaseew01 commented 9 years ago

Okay, so it does exist, I can read the content of the file on the hard drive from within the callback. Here, I'll commit real quick, maybe I'm just redirecting incorrectly or something.

jaseew01 commented 9 years ago

I accidentally committed to Issue 13

skiadas commented 9 years ago

Okay the first problem I encountered was a database error. Is your code creating the database if it doesn't already exist?

I think your redirect should not have the '/file' in front, it should start with '/downloads'

jaseew01 commented 9 years ago

No, it just assumes that the database is already created, I will have to take care of that. The filecollection database should've been on this repository though.

jaseew01 commented 9 years ago

Okay, so my redirect now looks like this:

res.redirect('/file/'+filename);

jaseew01 commented 9 years ago

it still doesn't seem to be working.

skiadas commented 9 years ago

Not '/file'!!! '/downloads' is what you want. You are redirecting them to a "downloads" resource, not a "file" resource.

I do see a fileCollection file on my comp, I wonder why it's not being accessed. Perhaps something wrong with the database program on my comp, I'll try to dig deeper.

jaseew01 commented 9 years ago

Sorry, that is what I currently have, I just didn't type it all. So this is what I currently have: res.redirect('/file/downloads/'+filename);

skiadas commented 9 years ago

No you should NOT have the '/file' in the front!!! It should start from the '/downloads'. Only downloads, no file in front.

jaseew01 commented 9 years ago

Sorry, fixed it, the browser is still showing me the same message: Cannot GET /downloadsDocument3.txt

jaseew01 commented 9 years ago

Cannot GET /downloads/Document3.txt

skiadas commented 9 years ago

Okay, now shouldn't that have been: /downloads/Document3.txt, i.e. with a slash between downloads and the filename?

jaseew01 commented 9 years ago

Yes, I am full of typos today, that is what I have on my server.

skiadas commented 9 years ago

Okay, I think you'll have to try to find what part of your code is receiving that request, see how far it makes it before it errors like that. Are you sure you are using express.static properly? Have a look at: http://blog.modulus.io/nodejs-and-express-static-content

The way I see it you need two tell it two things:

  1. The directory where the static content is at
  2. The request path to redirect to that static content. This will default to '/', while you want something like '/downloads'.

My guess is that as you have it now, a request to '/Document3.txt', without the /downloads in front, will succeed. Read that article to get the correct syntax.

jaseew01 commented 9 years ago

Okay, I will read over it all again, thanks for the help