Need some help understanding file_field and local_path

Hi all,

I’ve tried very hard to understand file_field and how to determine
local_path. However, I’m running into some issues and I believe it’s
probably due to the different behaviors of file_field and local_path
together.

First, here’s my issue.

I’m successfully parsing a yaml file using the following code (supplying
only what’s necessary here):

I’ll describe what I’m doing step by step and what I’ve accomplished so
far.

(The Task)

Admin user wants to upload a new theme. A new theme houses the
following file/directory structure:

– blue_lagoon (folder and the theme name)
|
– images (folder)
– javascripts (folder)
– layouts (folder)
– stylesheets (folder)
– swfs (folder)
blue_lagoon.yml (yaml config file that houses some information)

  1. Admin user goes to themes index and clicks new. (working)
  2. Admin user clicks browse and selects the blue_lagoon.yml file.
    (working)
  3. Admin user clicks upload. (working)
  4. Many validation checks are performed to ensure that a yml file is
    being selected, and that it contains all of the pertinent information.
    If it fails validation, notifications are sent to the admin user.
    (working)
  5. Once everything is validated, the yml file is moved by rails to a
    temporary folder and parsed. The information is then stored in a merged
    parameter and sent to the model to be saved. (working)
  6. The theme model now contains a complete record of the
    blue_lagoon.yml file. (working)
    ----- so far so good
    ----- here is where I’m running into some issues now
  7. Now I want to call an after_save method that will dir glob the
    location that the (original) blue_lagoon.yml file was uploaded from, and
    begin to mass create and upload the information to the public/themes
    folder.

The problem is I don’t know how to find the local_path to the current
upload because rails moved it to the temp folder. If I try:

@files = params[:theme_upload][:upload].local_path

… it will return the temp directory path, not the true path.

Any help would be appreciated as well as any advice going forward on
this topic.

Thanks.

On Jan 13, 11:22Â pm, Alpha B. [email protected] wrote:

  1. Â Now I want to call an after_save method that will dir glob the
    location that the (original) blue_lagoon.yml file was uploaded from, and
    begin to mass create and upload the information to the public/themes
    folder.

The problem is I don’t know how to find the local_path to the current
upload because rails moved it to the temp folder.

I’m confused. Rails saves the file in a temporary folder as soon as
the upload’s finished. (Also, under certain situations (which I
honestly don’t know off the top of my head) it might not bother with
the temporary file and store the uploaded data in a StringIO object.)
The file doesn’t exist on the server until the web server finishes
processing the post request, and Rails or Rack or whatever saves the
information where it wants. There is no “original” file.

Please correct me if I’m wrong, but it sounds like you want the path
of the file from the user’s machine?

pharrington wrote:

I’m confused. Rails saves the file in a temporary folder as soon as
the upload’s finished. (Also, under certain situations (which I
honestly don’t know off the top of my head) it might not bother with
the temporary file and store the uploaded data in a StringIO object.)
The file doesn’t exist on the server until the web server finishes
processing the post request, and Rails or Rack or whatever saves the
information where it wants. There is no “original” file.

Please correct me if I’m wrong, but it sounds like you want the path
of the file from the user’s machine?

That’s correct.

What I would like to do is mass upload everything that is globbed within
that directory. Therefore I need the exact path of the file where
original uploading began.

Example:

I click upload:

I browse to a file located in say C:\myfolder\mytheme\mytheme.yml

The structure of that folder is:

C:\myfolder\mytheme\images*
C:\myfolder\mytheme\layouts*
C:\myfolder\mytheme\javascripts*
C:\myfolder\mytheme\stylesheets*
C:\myfolder\mytheme\swfs*
C:\myfolder\mytheme\mytheme.yml

The first step was to read the mytheme.yml file as a manifest for what
will be usable/uploaded for the theme. In some cases, certain themes
might not include swfs, javascripts, or even layouts. The yml is read
and placed into the database model as a record manifest for the theme.

After the yml manifest is saved, I want to go back to the original
folder where the browse/upload began and then glob the structure to make
sure it matches the manifest and then I’ll create directories in the
public\themes folder of the application and copy the file structure from
that root location exactly.

How can I accomplish this? From other real-web examples, I see multiple
file uploads but they all have javascript attached to them that provide
multiple file_field inputs. I’m looking to mass glob upload files based
off a directory location.

On Jan 14, 2:19Â pm, Alpha B. [email protected] wrote:

pharrington wrote:

How can I accomplish this? Â From other real-web examples, I see multiple
file uploads but they all have javascript attached to them that provide
multiple file_field inputs. Â I’m looking to mass glob upload files based
off a directory location.

You can’t (at least not with plain html + javascript) - you don’t have
access to the user’s filesystem. I think that HTML5 adds some stuff in
this area (not sure what changes are and what has actually been
implemented)

Fred

Frederick C. wrote:

On Jan 14, 2:19Â pm, Alpha B. [email protected] wrote:

pharrington wrote:

How can I accomplish this? Â From other real-web examples, I see multiple
file uploads but they all have javascript attached to them that provide
multiple file_field inputs. Â I’m looking to mass glob upload files based
off a directory location.

You can’t (at least not with plain html + javascript) - you don’t have
access to the user’s filesystem. I think that HTML5 adds some stuff in
this area (not sure what changes are and what has actually been
implemented)

Fred

Thanks Fred, that’s what I thought. Well that’s not a big issue then.
In most cases if someone wanted to add a completely new theme to their
site, they would have to upload the files themselves anyways. As long
as I’m able to see the yml manifest and understand what’s in the theme
and how it’s being used, I should be okay.

Thanks for the clarification.

However, if someone has the time, I’d still like to know how I can find
the user directory that someone was attempting to load from. I’d like
to know this for future reference.

Take care.

On Jan 14, 10:20Â am, Alpha B. [email protected] wrote:

Thanks for the clarification.

However, if someone has the time, I’d still like to know how I can find
the user directory that someone was attempting to load from. Â I’d like
to know this for future reference.

I don’t believe that most browsers send that info - as a matter of
fact, I believe that IE’s sending of a full path (rather than the
plain filename) is regarded as a bug, rather than a feature. See, for
example, this ticket:

http://issues.apache.org/jira/browse/FILEUPLOAD-122

(from Jboss, but I remember seeing a comment about this in paperclip/
attachment_fu/etc. as well)

–Matt J.

Alpha B. wrote:
[…]

However, if someone has the time, I’d still like to know how I can find
the user directory that someone was attempting to load from. I’d like
to know this for future reference.

There is no reason that you or your app should have any interest in the
particulars of your client’s filesystem. It is, quite frankly, none of
your app’s business, just as your server’s filesystem is none of the
client’s business.

Take care.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Fri, Jan 15, 2010 at 6:35 AM, Marnen Laibow-Koser
[email protected]wrote:

Marnen is correct here. Please understand that while I understand that
you
may be interested
in doing more work so the user does less - it is wiser to not get to
interested about the users filesystem
and I would encourage you to only accept ZIP files in this case and
spend
the time validating the directory
structure and any requried files and assist the user via docs and or
some
step by step wizard ( for newbs )
to make your theme file. I expect anyone that can make said theme is
actually very competent.

Know your audience.

On Jan 15, 3:32Â pm, Alpha B. [email protected] wrote:

I don’t need to implement the upload feature for bulk files any longer.
All I use now is an upload to the yml configuration file for the theme.
My question was asked more out of curiosity. Â Rails needed to know the
original path to move the file to the temporary path. Â I was only
curious what method(s) Rails used to perform that scenario.

It doesn’t - the webserver gets a stream of bytes from the client
(with some metadata) and plonks those bytes somewhere temporary (or
not at all for small uploads).

Fred

I don’t need to implement the upload feature for bulk files any longer.
All I use now is an upload to the yml configuration file for the theme.
My question was asked more out of curiosity. Rails needed to know the
original path to move the file to the temporary path. I was only
curious what method(s) Rails used to perform that scenario.

But, again, I only asked out of curiosity.

Not to mention, your code is on the server. You can’t access the
client’s file system from the server anyway. Dir.glob would fail
because you’d be globbing your own disk.

Frederick C. wrote:

It doesn’t - the webserver gets a stream of bytes from the client
(with some metadata) and plonks those bytes somewhere temporary (or
not at all for small uploads).

Fred

Thanks Fred, that satisfies my curiosity.