Changeset 4328


Ignore:
Timestamp:
Nov 20, 2009, 10:19:04 PM (11 years ago)
Author:
plg
Message:

improvement: to prepare feature 1051, the code to add a single file (works for
the 3 picture types, ie thumb/file/high) has been factorized into a single
add_file function.

bug fixed: when a function not directly called by the API (such as merge_chunks
or the new add_file function) returning a PwgError, it was not stopping the
execution, it just sets the error code/message. Now we don't return PwgError,
we only create one and then exit().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0/include/ws_functions.inc.php

    r3661 r4328  
    974974    if (!file_put_contents($output_filepath, $string, FILE_APPEND))
    975975    {
    976       return new PwgError(500, 'error while writting chunks for '.$output_filepath);
     976      new PwgError(500, '[merge_chunks] error while writting chunks for '.$output_filepath);
     977      exit();
    977978    }
    978979   
     
    981982
    982983  ws_logfile('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage());
     984}
     985
     986/*
     987 * The $file_path must be the path of the basic "web sized" photo
     988 * The $type value will automatically modify the $file_path to the corresponding file
     989 */
     990function add_file($file_path, $type, $original_sum, $file_sum)
     991{
     992  // resolve the $file_path depending on the $type
     993  if ('thumb' == $type) {
     994    $file_path = get_thumbnail_location(
     995      array(
     996        'path' => $file_path,
     997        'tn_ext' => 'jpg',
     998        )
     999      );
     1000  }
     1001
     1002  if ('high' == $type) {
     1003    @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
     1004    $file_path = get_high_location(
     1005      array(
     1006        'path' => $file_path,
     1007        'has_high' => 'true'
     1008        )
     1009      );
     1010  }
     1011
     1012  $upload_dir = dirname($file_path);
     1013 
     1014  if (!is_dir($upload_dir)) {
     1015    umask(0000);
     1016    $recursive = true;
     1017    if (!@mkdir($upload_dir, 0777, $recursive))
     1018    {
     1019      new PwgError(500, '[add_file] error during '.$type.' directory creation');
     1020      exit();
     1021    }
     1022  }
     1023
     1024  if (!is_writable($upload_dir))
     1025  {
     1026    // last chance to make the directory writable
     1027    @chmod($upload_dir, 0777);
     1028
     1029    if (!is_writable($upload_dir))
     1030    {
     1031      new PwgError(500, '[add_file] '.$type.' directory has no write access');
     1032      exit();
     1033    }
     1034  }
     1035
     1036  secure_directory($upload_dir);
     1037
     1038  // merge the thumbnail
     1039  merge_chunks($file_path, $original_sum, $type);
     1040  chmod($file_path, 0644);
     1041
     1042  // check dumped thumbnail md5
     1043  $dumped_md5 = md5_file($file_path);
     1044  if ($dumped_md5 != $file_sum) {
     1045    new PwgError(500, '[add_file] '.$type.' transfer failed');
     1046    exit();
     1047  }
     1048
     1049  list($width, $height) = getimagesize($file_path);
     1050  $filesize = floor(filesize($file_path)/1024);
     1051
     1052  return array(
     1053    'width' => $width,
     1054    'height' => $height,
     1055    'filesize' => $filesize,
     1056    );
    9831057}
    9841058
     
    10251099    );
    10261100
    1027   // create the upload directory tree if not exists
    1028   if (!is_dir($upload_dir)) {
    1029     umask(0000);
    1030     $recursive = true;
    1031     if (!@mkdir($upload_dir, 0777, $recursive))
    1032     {
    1033       return new PwgError(500, 'error during directory creation');
    1034     }
    1035   }
    1036 
    1037   if (!is_writable($upload_dir))
    1038   {
    1039     // last chance to make the directory writable
    1040     @chmod($upload_dir, 0777);
    1041 
    1042     if (!is_writable($upload_dir))
    1043     {
    1044       return new PwgError(500, 'directory has no write access');
    1045     }
    1046   }
    1047 
    1048   secure_directory($upload_dir);
    1049 
    10501101  // compute file path
    10511102  $date_string = preg_replace('/[^\d]/', '', $dbnow);
     
    10541105  $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg';
    10551106
    1056   // merge the photo file
    1057   merge_chunks($file_path, $params['original_sum'], 'file');
    1058   chmod($file_path, 0644);
    1059 
    1060   // check dumped file md5sum against expected md5sum
    1061   $dumped_md5 = md5_file($file_path);
    1062   if ($dumped_md5 != $params['file_sum']) {
    1063     return new PwgError(500, 'file transfer failed');
    1064   }
    1065 
    1066   // thumbnail directory is a subdirectory of the photo file, hard coded
    1067   // "thumbnail"
    1068   $thumbnail_dir = $upload_dir.'/thumbnail';
    1069   if (!is_dir($thumbnail_dir)) {
    1070     umask(0000);
    1071     if (!@mkdir($thumbnail_dir, 0777))
    1072     {
    1073       return new PwgError(500, 'error during thumbnail directory creation');
    1074     }
    1075   }
    1076 
    1077   if (!is_writable($thumbnail_dir))
    1078   {
    1079     // last chance to make the directory writable
    1080     @chmod($thumbnail_dir, 0777);
    1081 
    1082     if (!is_writable($thumbnail_dir))
    1083     {
    1084       return new PwgError(500, 'thumbnail directory has no write access');
    1085     }
    1086   }
    1087 
    1088   secure_directory($thumbnail_dir);
    1089 
    1090   // thumbnail path, the filename may use a prefix and the extension is
    1091   // always "jpg" (no matter what the real file format is)
    1092   $thumbnail_path = sprintf(
    1093     '%s/%s%s.%s',
    1094     $thumbnail_dir,
    1095     $conf['prefix_thumbnail'],
    1096     $filename_wo_ext,
    1097     'jpg'
    1098     );
    1099 
    1100   // merge the thumbnail
    1101   merge_chunks($thumbnail_path, $params['original_sum'], 'thumb');
    1102   chmod($thumbnail_path, 0644);
    1103 
    1104   // check dumped thumbnail md5
    1105   $dumped_md5 = md5_file($thumbnail_path);
    1106   if ($dumped_md5 != $params['thumbnail_sum']) {
    1107     return new PwgError(500, 'thumbnail transfer failed');
    1108   }
    1109 
    1110   // high resolution
     1107  // add files
     1108  $file_infos  = add_file($file_path, 'file',  $params['original_sum'], $params['file_sum']);
     1109  $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']);
     1110
    11111111  if (isset($params['high_sum']))
    11121112  {
    1113     // high resolution directory is a subdirectory of the photo file, hard
    1114     // coded "pwg_high"
    1115     $high_dir = $upload_dir.'/pwg_high';
    1116     if (!is_dir($high_dir)) {
    1117       umask(0000);
    1118       if (!@mkdir($high_dir, 0777))
    1119       {
    1120         return new PwgError(500, 'error during high directory creation');
    1121       }
    1122     }
    1123 
    1124     if (!is_writable($high_dir))
    1125     {
    1126       // last chance to make the directory writable
    1127       @chmod($high_dir, 0777);
    1128 
    1129       if (!is_writable($high_dir))
    1130       {
    1131         return new PwgError(500, 'high directory has no write access');
    1132       }
    1133     }
    1134 
    1135     secure_directory($high_dir);
    1136 
    1137     // high resolution path, same name as web size file
    1138     $high_path = sprintf(
    1139       '%s/%s.%s',
    1140       $high_dir,
    1141       $filename_wo_ext,
    1142       'jpg'
    1143       );
    1144 
    1145     // merge the high resolution file
    1146     merge_chunks($high_path, $params['original_sum'], 'high');
    1147     chmod($high_path, 0644);
    1148 
    1149     // check dumped thumbnail md5
    1150     $dumped_md5 = md5_file($high_path);
    1151     if ($dumped_md5 != $params['high_sum']) {
    1152       return new PwgError(500, 'high resolution transfer failed');
    1153     }
    1154 
    1155     $high_filesize = floor(filesize($high_path)/1024);
    1156   }
    1157 
    1158   list($width, $height) = getimagesize($file_path);
     1113    $high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']);
     1114  }
    11591115
    11601116  // database registration
     
    11651121    'name' => $params['name'],
    11661122    'path' => $file_path,
    1167     'filesize' => floor(filesize($file_path)/1024),
    1168     'width' => $width,
    1169     'height' => $height,
     1123    'filesize' => $file_infos['filesize'],
     1124    'width' => $file_infos['width'],
     1125    'height' => $file_infos['height'],
    11701126    'md5sum' => $params['original_sum'],
    11711127    );
     
    11901146  {
    11911147    $insert['has_high'] = 'true';
    1192     $insert['high_filesize'] = $high_filesize;
     1148    $insert['high_filesize'] = $high_infos['filesize'];
    11931149  }
    11941150
Note: See TracChangeset for help on using the changeset viewer.