Jamie,

finally back working on drupal code again (after a few months away) and started playing with your latest fileshare code. Looks like you've done a lot with it, great work!

Any ways, uploading some files with long filenames I noticed that the extensions were being lost (anything after the 50 character limit was cut). Additionally, a file called "filename.psd" would end up as "filenamepsd" because the module removes the "." characters as well, which creates problems after downloading if the OS can't figure out what filetype to use without an extension.

So I updated the _sanitize_filename function to try handle those situations. Not nearly as short and pretty as what you had, but it seems to do the trick. By the way, is there any particular reason for the 50 character limit? Could I extend that limit without messing up some check you had in mind?

Patrick

/**
 * Returns a filename based on the $name paramater that has been
 * striped of special characters, it's spaces changed to underscores,
 * and shortened to 50 characters... but keeping it's extension
 * PD: Updated to keep extensions, based on code by timdw at
 * http://forums.codecharge.com/posts.php?post_id=75694
 */
function _sanitize_filename($name) {
  $special_chars = array ("#","$","%","^","&","*","!","~","‘","\"","’","'","=","?","/","[","]","(",")","|","<",">",";","\\",",",".");
  $name = preg_replace("/^[.]*/","",$name); // remove leading dots
  $name = preg_replace("/[.]*$/","",$name); // remove trailing dots
  
  $lastdotpos=strrpos($name, "."); // save last dot position
  
  $name = str_replace($special_chars, "", $name);  // remove special characters
  
  $name = str_replace(' ','_',$name); // replace spaces with _
  
  $afterdot = "";
  if ($lastdotpos !== false) { // Split into name and extension, if any.
	if ($lastdotpos < (strlen($name) - 1))
		$afterdot = substr($name, $lastdotpos);
    
	$extensionlen = strlen($afterdot);
	
    if ($lastdotpos < (50 - $extensionlen) )
		$beforedot = substr($name, 0, $lastdotpos);
	else
		$beforedot = substr($name, 0, (50 - $extensionlen));
  }
  else   // no extension
   $beforedot = substr($name,0,50);
 
  
  if ($afterdot)
	$name = $beforedot . "." . $afterdot;
  else
	$name = $beforedot;
  
  return $name;

}

Comments

pcdonohue’s picture

Another thought would be to not consider the "." as a special character given that a lot of filenames might have multiple dots in them, fileshare-4.7.x-1.x-dev.tar.gz for example. Of course, the extension would still be lost if the filename went over the 50 character limit.

Patrick

JamieR’s picture

Patrick... thanks for looking out for me... unfortunately I can't keep up with the changed in the module distribution that they keep making. I used to have the HEAD version of the module as the default release... but now it's changed! These fixes and more are in that release:

http://cvs.drupal.org/viewcvs/drupal/contributions/modules/fileshare/?on...

I've come to hate cvs. Now a days I spend more time on this project, trying to figure out cvs than working on the module. :(

Anyone know how to move my HEAD code into 4.7.x-2.0?
I hate cvs.

Jamie.

pcdonohue’s picture

Status: Needs review » Fixed

Hi Jamie,

your fixes in HEAD seem to work just fine.

It's possible I'm running into another issue now with the preview button not showing up in my browser when trying to create a new fileshare (I use Camino normally, though Safari doesn't seem to have a problem), but I'll look into some more and post a separate issue if necessary.

thanks!
Patrick

Anonymous’s picture

Status: Fixed » Closed (fixed)
jonahan’s picture

This is still an issue in 5.2.

After some research I used http://drupal.org/project/file_translit. Worked like a charm! Thankya big!