There are 2 questions.

1. In previous versions mp_file I changed the function mp_file_commit, adding:

$ new_name = $ node-> uid .'_'. $ node-> nid .'_'. $ sanitized_name .'.'. $ ext;"

Files renamed by this mask: uid_nid_nodename.ext. The latest version mp_file I could not get myself to repeat it. Could you help to repeat it?

2. I have already said that the use of Cyrillic characters file name is blank. I propose to add/change to mp_file after 285-line the following code:

  / / Rename file ...

if (module_exists ( 'transliteration')) (
  $ transliterated = transliteration_get ($ title);
)

  $ node_sanitized_title = preg_replace ('/[^ 0-9a-z \. \ _ \ -] / i ','', $ transliterated);

Thanks.

CommentFileSizeAuthor
#5 keereel.zip541 bytessyndicateStorm

Comments

syndicateStorm’s picture

Hi keereel, I'm on the verge of a 6.x production release, so this is a good time to get this incorporated. Let's see if we can get this and your other request(s) marked off as "fixed".

Unfortunately you will have to get the latest dev release (you might have to wait until this afternoon for it to come out) as I've made a few minor changes to help incorporate your request. Your request should now be supported by a new "hook" which will hopefully allow you to never have to make these changes again :-).

Do you have any custom modules you've written for your site? I typically have at least one called "site_helper" where I can put hooks and Drupal modifications, etc. If you need help setting one up I can help but you can probably find a lot of resources online which can help you figure this out.

In this "site_helper" module (or whatever you call it) paste in the following hook function:

/**
 * Implements hook_mp_file_name().
 *
 * Returns the filename to be used for this feature.  
 */
function site_helper_mp_file_name($dir, $file_name, $file_name_ext, $node_id, $title) {
  $node_sanitized_title = preg_replace('/[^0-9a-z\.\_\-]/i', '', $title);
  
  $new_file_name = $node_id . "_" . $file_name . "." . $file_name_ext;

  // We could now return $new_file_name but first let's rename it if this file name already exists.
  // NB: $file_name also has a counter in it for files in the temp directory
  $i_file = 0;
  while (file_exists($dir . $new_file_name)) {
    $i_file++;
    $new_file_name = $node_id . "_" . $file_name . "_" . sprintf("%03d", $i_file) . "." . $file_name_ext;
  }

  return $new_file_name;
}

If you leave the above code unmodified, mp_file should behave exactly the same. At this point you can modify this function to do what you want.

So here is some untested code that might work for you:

/**
 * Implements hook_mp_file_name().
 *
 * Returns the filename to be used for this feature.  
 */
function site_helper_mp_file_name($dir, $file_name, $file_name_ext, $node_id, $title) {
  if (module_exists ('transliteration')) {
    $transliterated = transliteration_get ($title);
  }
  $node_sanitized_title = preg_replace('/[^0-9a-z\.\_\-]/i','', $transliterated);
  
  $node = node_load($nid);
  $new_file_name = $node->uid .'_'. $node_id .'_'. $node_sanitized_title .'.'. $file_name_ext;

  return $new_file_name;
}

The above may not work and probably doesn't do exactly what you want. But hopefully it's enough to get you started. Let me know if I can help.

keereel’s picture

Status: Active » Closed (fixed)

> I'm on the verge of a 6.x production release.

This is good news. Thank you for the direction indicated, I will try to do it. All of my issues may be switched to the closed position, if it will facilitate release of the module. My questions are not so critical, that would delay the release, they can be resolved later.

keereel’s picture

Status: Closed (fixed) » Patch (to be ported)

Maybe someone will find useful: I have done that to get the filename as uid_nid_filename.ext (all in mp_file.module):
line 289:

  $new_name_hook = module_invoke_all('mp_file_name', $dir, $file_name, $file_name_ext, $node->nid, $node_sanitized_title);

to

  $new_name_hook = module_invoke_all('mp_file_name', $dir, $file_name, $file_name_ext, $node->uid, $node->nid, $node_sanitized_title);

lines 313-314:

function mp_file_mp_file_name($dir, $file_name, $file_name_ext, $node_id, $node_sanitized_title) {
  $new_file_name = $node_id . "_" . $file_name . "." . $file_name_ext;

to

function mp_file_mp_file_name($dir, $file_name, $file_name_ext, $node_uid, $node_id, $node_sanitized_title) {
  $new_file_name = $node_uid . "_" . $node_id . "_" . $file_name . "." . $file_name_ext;

Is this correct?

syndicateStorm’s picture

keereel, looks reasonable to me. However, I have a few thoughts:

* Sorry for making you download marketplace over and over, but make sure you have the Feb 14th release or later. I made a minor change specifically for you, that is included in that release and is consistent with post #1.

* I didn't test the code in #1 but you should be able to get the uid from the nid, right? Did you try that (see #1)?

* You should be able to do this all outside mp_file in your own custom module using hook_mp_file_name. I would imagine this would be preferable to editing mp_file.module unless you need faster execution or something? I think the second code snippet in #1 does almost everything you describe (it made need a little work but I think it's all there).

I look forward to hearing your thoughts. Cheers.

syndicateStorm’s picture

StatusFileSize
new541 bytes

Well there is always something wrong with untested code :-). I used node_load($nid) but it should have been node_load($node_id). I have attached a custom module that does what you have requested. Here's all it is:

function keereel_mp_file_name($dir, $file_name, $file_name_ext, $node_id, $title) {
  $node = node_load($node_id);
  $new_file_name = $node->uid .'_'. $node_id .'_'. $file_name .'.'. $file_name_ext;

  return $new_file_name;
}

To use the attached module just put it in your modules folder and enable it just like you do any other module. Let me know how it goes.

keereel’s picture

Sorry for the delay. Dentist caught me, and he took all my time. I hope to soon get out of his hands and continue the experiments.

keereel.zip - it is site_helper module?

syndicateStorm’s picture

No problem. Yes keereel.zip is site_helper. Although I changed the module name from "site_helper" to "keereel". You can change it back to site_helper if you want. Good luck.

keereel’s picture

Great, it works with #5 code in site_helper module! One more thing - in new_file_name we use file_name, but I think, node title is better. How can I make this?

syndicateStorm’s picture

Great! Try $node->title.

3dloco’s picture

Hello,

I noticed that you are modifying the code to rename files and I'd like to know whether you tried to use the filefield paths module to achieve this.

I am currently using filefield paths to rename, cleanup, and move files that are uploaded to the site. However, it is not working well with Marketplace. I noticed that renaming with filefield paths is ok for every CCK filefield except the one for Marketplace product files.

Is this something that needs to be configured/modified somewhere?

Thanks,

KH

syndicateStorm’s picture

3dloco, interesting point. I'll take a look at the fielfield paths module and let you know what I learn. Thanks.

syndicateStorm’s picture

Status: Patch (to be ported) » Fixed

keereel, I think we're done here? 3dloco, we can discuss fiefield paths here: http://drupal.org/node/724372.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

keereel’s picture

Version: 6.x-1.x-dev » 6.x-1.0-beta1
Status: Closed (fixed) » Active

Something changes in v.1.0.b1? My 'site helper' module (from #5) don't work now.

P.S. Is it possible to make field for 'filename' option with tokens in module configuration?

syndicateStorm’s picture

Status: Active » Fixed

Hey keereel, I believe the issue pertains to module weight. I had incorrectly set up the weighting system, but corrected it before releasing beta1. Make sure your module weight for site_helper is bigger than UC Marketplace. Let me know how it works for you.

keereel’s picture

Hi.
If MP have weight '0', I must just set weight '1' for my 'helper' module, right? In 'system' table..

syndicateStorm’s picture

That should work. Did it? As long as MP < helper it should work. Basically the largest weight module will be executed last and will therefore have the last say on how the renaming should be done.

keereel’s picture

yes, thank you!

Status: Fixed » Closed (fixed)
Issue tags: -renaming files, -ubercart marketplace, -cck filefield

Automatically closed -- issue fixed for 2 weeks with no activity.