Let's say for filename myvideo.mov.flv or myvideo.mov.avi.flv, 1st 'extention' .mov is detected and the file may not be processed. But in actual fact the last extension should be returned.

Attached is the patch that fix that, make use of $path_parts['filename'] only for PHP5 and above, and a fall back for PHP4 is included.

CommentFileSizeAuthor
media_mover_extension.patch1.07 KBckng

Comments

arthurf’s picture

Priority: Critical » Normal

Thanks for the patch. The problem with this whole thing is that users can name their files what ever they please and it is really hard to figure out what is valid and what needs to be removed. I took a different approach than you did and removed all strings that match '.12345' assuming that file extensions are always 5 or smaller (eg: the improper mpeg4). This not perfect- really, we should use mime detect's list of file extensions and strip off anything that matches the mime detect functions, but there isn't currently a function in mime detect to do this (see: http://drupal.org/node/342189). Here's the solution I came up with:

  // file name for demonstration purposes
  $current_file = 'myvideo.mynewvid.mov.avi.flv.mpeg4';
  // Construct the file name
  // we need to strip out any existing file extensions. The
  // problem with this approach is that it assumes that extension names
  // are always 5 or less characters. It could be the case that there
  // is a file name like: myvideo.char.mov.avi.flv.mpeg4 which
  // will have 'char' removed.
  $file_name_parts = explode('.', $current_file);
  foreach ($file_name_parts as $part) {
    if (strlen($part) >= 5) {
      $final_name[] = $part;      
    }
    else{
      break;
    }
  }
  $output_file = implode('.', $final_name);
  print $output_file;

Any thoughts?

delykj’s picture

Also, would be great if you could use the Transliteration module to normalize filenames:
http://drupal.org/project/transliteration

ckng’s picture

I think we don't need to remove other 'extensions' as it could be really part of a filename, and we can't control what user going to do with the filename or extension.
My suggestion would be to just take the last .ext. However, we can add in additional checking to make sure that the mime detected format equals the last .ext., and give a warning if they do not match.

ckng’s picture

Correct me if I'm wrong, from my understanding transliteration should work transparently when an upload occurred. Don't think we need to explicitly transliterate here.

arthurf’s picture

ckng- where you say:

Let's say for filename myvideo.mov.flv or myvideo.mov.avi.flv, 1st 'extention' .mov is detected and the file may not be processed

From what I understand of your concerns here, you want all extensions removed, not just the last one, correct? In this case we either have to guess what the extensions are, or we have to just use the last one.

So here's my propsal: we just do str_replace('.', '_', $filename) and append $new_extension on. This way we leave the "filename" intact, while avoiding the above concern.

How's that sound?

ckng’s picture

Actually, from my point of view, only last extension needs to be removed. The rest are IMHO user choice on naming their files.

And my example is wrong actually, let me try to explain again =)

For a conversion configuration of MOV => FLV
Take myvideo.flv.sometext.avi.mov as example - original code will stop at .flv and remove the rest of 'extension' leaving myvideo.flv and consider the file processed. IMO this is a wrong approach, in normal sense, myvideo.flv.sometext.avi should be the filename and .mov the extension, as any OS see it. So that's what my patch trying to do - only taking in the last extension (.mov) and leave the rest as it is (filename - myvideo.flv.sometext.avi). And the processed file will become myvideo.flv.sometext.avi.flv

Is that clearer?

As a side note, jquery media has the exact same problem, which should be fixed in its next release.

arthurf’s picture

So would you be happy with something like:

  $file = 'myvideo.flv.sometext.avi.mov';
  $pattern = '/(.*)\..*$/';
  $replace = '$1.flv';
  print_r(preg_replace($pattern, $replace, $file));

While your solution is totally workable, I feel like this kind of approach is a bit cleaner- granted regexs are harder to read, but I think keeps things a bit more obvious

ckng’s picture

Regex will do as well.

arthurf’s picture

Status: Needs review » Fixed

Slight change to change white space to '_' as well. Fix is committed to D6 and D5. Thanks for the help!

Status: Fixed » Closed (fixed)

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