When migrating content from other CMS to Drupal with Migrate & Migrate Extras modules, I expected that the filenames were transliterated for FileFields (specially because of many of them were FLVs to be played with Flowplayer that rejects URIs with spaces).

I made a quick patch (against 6.x-3.7) to tell FileField to use the Transliteration module if:

  1. module installed and the transliterate file uploads option is enabled
  2. if the call to field_file_save_file() (in field_file.inc) isn't originated by a HTTP request, checking the $_FILES superglobal

A test method was added to filefield.test to verify the correcteness of the code.

CommentFileSizeAuthor
filefield.transliterate_when_file_created_programatically.patch3.53 KBAnonymous (not verified)

Comments

quicksketch’s picture

Hmm, my preference when saving files programmatically like this is to call the transliterate functions directly before saving the file. As a programmer I expect my files to be renamed unless I ask them to be renamed.

Anonymous’s picture

When I made this, I was thinking in consistency. Actually, isn't the better way because if I just used the file API, it would not transliterate the filenames also and would it only if the core were hacked.

I thought FileField would be the better place as other modules could use it to submit files programatically in migration tasks.

Because the way Transliteration works (using hook_init() to transliterate the filenames), it seems too much to require this consistency from FileField. However, how can I transliterate the $file->filename if the stdClass object is created inside field_file_save_file()? In a migration task, I would need to prepare the files, maybe copying and renaming but for larger and complex migrations, this could be a very time consuming task.

A simpler approach would be an extra parameter for the filename. I don't know if extra parameters make your taste but seems more affordable to me than making a change full of side-effects like I did in my patch.

quicksketch’s picture

Status: Needs review » Closed (won't fix)

I think the simplest approach is simply manually calling transliteration_clean_filename() if you want it to clean the file name before you save the file at all. There's no point in adding a parameter to to FileField when it doesn't do anything but call a function in Transliteration module.