Configurable file save path directives file_field.inc and image_field.inc

dgtlmoon - March 29, 2006 - 03:18
Project:Flexinode
Version:HEAD
Component:flexinode.module (core)
Category:feature request
Priority:normal
Assigned:dgtlmoon
Status:needs work
Description

This patch allows you to specify a subdirectory description of how you'de like the files to be saved. Handy for when different node types need to save the file to disk in different ways

The pathwill be appended to $_GLOBAL['file_directory_path'], mark it up with %username, %day, %dayname, %month, %monthname, %year . Blank if not required. Directories will be created, default (blank)

Example: somenodefiles/%year/%monthname/%username.

Will save as

$GLOBAL['file_directory_path']."/somenodefiles/2006/January/admin/somefile.pdf"

All filenames are preserved.

#1

dgtlmoon - March 29, 2006 - 03:20

No idea if or where the patch i attached to this node went so here it is for good measure

--- flexinode-cvs/field_file.inc        2005-11-10 20:35:16.000000000 +1100
+++ flexinode/field_file.inc    2006-03-29 14:17:06.000000000 +1100
@@ -15,6 +15,7 @@
     '#required' => $field->required,
     '#weight' => $field->weight,
     );
+
   $form[$fieldname .'_old'] = array(
     '#type' => 'hidden',
     '#value' => serialize($node->$fieldname),
@@ -32,6 +33,7 @@
}

function flexinode_field_file_insert($field, $node) {
+
   $fieldname = 'flexinode_'. $field->field_id;
   $serialized = is_object($node->$fieldname) ? serialize($node->$fieldname) : '';
   db_query("INSERT INTO {flexinode_data} (nid, field_id, textual_data, serialized_data) VALUES (%d, %d, '%s', '%s')", $node->nid, $field->field_id, $node->$fieldname->filename, $serialized);
@@ -42,13 +44,61 @@
   $result = db_fetch_object(db_query('SELECT serialized_data FROM {flexinode_data} WHERE nid = %d AND field_id = %d', $node->nid, $field->field_id));
   $file = unserialize($result->serialized_data);
   if ($unconditional || $node->$fieldname != $file) {
-    file_delete($file->filepath);
   }
}

+
+// replaces save path tags with correct values
+function _save_path_markup($metatag) {
+    global $user;
+
+    // add any extra functionality here, such as %workflowstate etc
+    foreach($metatag as $tag) {
+       switch(strtolower($tag)) {
+           case "%day":
+               $ret= date("j");
+           break;
+           case "%dayname":
+               return date("l");
+           break;
+           case "%month":
+               return date("n");
+           break;
+           case "%monthname":
+               return date("F");
+           break;
+           case "%year":
+               return date("Y");
+           break;
+           case "%username":
+               return $user->name;
+           break;
+
+       };
+    }
+
+}
+
+
function flexinode_field_file_execute($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  if ($file = file_save_upload($fieldname, variable_get('file_directory_path', NULL))) {
+  $savepath=variable_get('file_directory_path', NULL);
+  if(is_array($field->options)) {
+    $path_extention=preg_replace_callback("/%.*?(?=(\W|$))/","_save_path_markup",implode("",$field->options));
+    // create the subdirectories if path_extention is set
+    foreach(explode("/",$path_extention) as $p) {
+      if($p!="/") {
+        $extpath.="/$p";
+           if(!file_exists($savepath."/".$extpath)) {
+               mkdir($savepath."/".$extpath);
+           }
+       }
+    }
+    $savepath.=$extpath;
+  }
+
+
+  if ($file = file_save_upload($fieldname, $savepath )) {
     return $file;
   }
   elseif (empty($node->$fieldname)) {
@@ -70,6 +120,16 @@
   return unserialize($node->$fieldname);
}

+function flexinode_field_file_config($field, $edit) {
+  return array('options' => array(
+      '#type' => 'textfield',
+      '#title' => 'Save path format',
+      '#default_value' => implode("",$field->options),
+      '#description' => t('Enter save path format, this will be appended to file_directory_path, mark it up with %username, %day, %dayname, %month, %monthname, %year . Blank if not required. Directories will be created, default (blank) <BR>Example: nodefiles/%year/%month/%username. All filenames are preserved.'),
+      ));
+}
+
+

/**
  * @addtogroup themeable

AttachmentSize
savepathformat.patch.txt 3.08 KB

#2

Bèr Kessels - April 18, 2006 - 09:14
Version:<none>» HEAD
Priority:normal» critical

This looks extremely usefull to me. Can someone review this?

#3

ahoeben - June 29, 2006 - 20:16
Priority:critical» normal

Patch works as advertised, but included needed cleanup. +1 for functionality

I moved the core functionality of the patch to flexinode.module, and rolled new patches for field_file.inc and field_image.inc. Image fields can now also set file save paths.

Attached tar contains three patches.

AttachmentSize
file_savepath.tar 10 KB

#4

ahoeben - June 30, 2006 - 20:39
Status:needs review» needs work

I'll be rolling new patches since my patches above are kind-off sort-off bogus. Must sleep more...

#5

ahoeben - June 30, 2006 - 22:17

patch 1 of 3

Patches flexinode.module with dgtlmoon's functionality. Adds '%userid' metapath element (user's uid). Usernames can e changed (by the user), at which time new directories would be created. I'm also not sure about special characters in the usernames. We'll probably have to add validation to the %username code.

AttachmentSize
flexinode_savepath.patch 10.47 KB

#6

ahoeben - June 30, 2006 - 22:20

patch 1 of 3

And again I screw up... Please disregard the previous patch
Here's the good patch for flexinode.module.

AttachmentSize
flexinode_savepath_0.patch 2.02 KB

#7

ahoeben - June 30, 2006 - 22:23

patch 2 of 3

Patches field_file to add the path directives. Also adds fields to optionally limit the filesize and extension (after upload).

AttachmentSize
file_savepath.patch 2.97 KB

#8

ahoeben - June 30, 2006 - 22:25

patch 3 of 3

Patches field_image to add the path directives.

Note: please apply all three patches.

AttachmentSize
image_savepath.patch 3.45 KB

#9

igrcic - July 5, 2006 - 17:44

can anyone please revert this for v 4.6 also ??

thank you so much

#10

dgtlmoon - July 6, 2006 - 00:26

havent used 4.6 :( sorry

has this been commited to HEAD branch?

#11

Bèr Kessels - July 7, 2006 - 09:52

Not committed. There are more pressing issues in queue for file fields.

#12

igrcic - July 12, 2006 - 23:16

this is so much needed function! can anyone try to make path for 4.6.* ??? some directions maybe?? tnx

#13

dgtlmoon - July 13, 2006 - 23:56

what errors do you get if you try to use it in 4.6?

#14

Bèr Kessels - July 14, 2006 - 07:24

Note that I won't add new features on 4.6, 4.7 or any other stable branch. Also note that this feature is therefore marked for CVS.

#15

dgtlmoon - September 6, 2006 - 05:39

Will this make it for 5.0? it is much needed

#16

Bèr Kessels - September 13, 2006 - 09:13

please give the patch another review, using the latest HEAD file fields.

#17

ahoeben - January 28, 2007 - 14:49
Title:Patch for flexinode file_field.inc to add configurable file save path directives» Configurable file save path directives file_field.inc and image_field.inc
Component:Code» flexinode.module (core)

Attached path is against 4.7.x 1.0, but needs some testing.

For file_field.inc, additional upload limitations were lifted from image_field.inc. A list of accepted extensions is a required option for the field, in order to increase security.

AttachmentSize
flexinode_savepath_1.patch 8.78 KB

#18

Bèr Kessels - January 28, 2007 - 15:43

Lets keep the focus and not mix up features.
This issue thread is about Configurable file save path directives file_field.inc and image_field.inc

I created another thread to allow file-extension settigns, find it in http://drupal.org/node/113506

#19

ahoeben - January 28, 2007 - 16:42

In order to keep consistency between image and file uploading, implementing the two features became rather intertwined.

 
 

Drupal is a registered trademark of Dries Buytaert.