Ignore:
Timestamp:
Jun 17, 2013, 5:42:09 PM (11 years ago)
Author:
mistic100
Message:

WORK IN PROGRESS: allow to download derivatives
TODO: only working with images files, finish download page

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/BatchDownloader/include/BatchDownloader.class.php

    r23280 r23290  
    1414   * @param: string set type ('album'|'tag'|'selection')
    1515   * @param: int set type id (for retrieving album infos for instance)
    16    */
    17   function __construct($set_id, $images=array(), $type=null, $type_id=null)
     16   * @param: string size to download
     17   */
     18  function __construct($set_id, $images=array(), $type=null, $type_id=null, $size='original')
    1819  {
    1920    global $user, $conf;
     
    2627      'type' => null,
    2728      'type_id' => null,
     29      'size' => 'original',
    2830      'nb_zip' => 0,
    2931      'last_zip' => 0,
     
    3941    {
    4042      $query = '
    41 SELECT
    42     id,
    43     user_id,
    44     date_creation,
    45     type,
    46     type_id,
    47     nb_zip,
    48     last_zip,
    49     nb_images,
    50     total_size,
    51     status
     43SELECT *
    5244  FROM '.BATCH_DOWNLOAD_TSETS.'
    5345  WHERE
     
    9587    else if ($set_id == 'new')
    9688    {
     89      if ($size != 'original')
     90      {
     91        $type_map = array_keys(ImageStdParams::get_defined_type_map());
     92        if (!in_array($size, $type_map))
     93        {
     94          throw new Exception(sprintf(l10n('Invalid size %s'), $size));
     95        }
     96      }
     97 
    9798      $this->data['type'] = $type;
    9899      $this->data['type_id'] = $type_id;
     100      $this->data['size'] = $size;
    99101     
    100102      $query = '
     
    104106    type,
    105107    type_id,
     108    size,
    106109    nb_zip,
    107110    last_zip,
     
    115118    "'.$this->data['type'].'",
    116119    "'.$this->data['type_id'].'",
     120    "'.$this->data['size'].'",
    117121    0,
    118122    0,
     
    255259 
    256260  /**
     261   * get missing derivatives files
     262   * @return: array of i.php urls
     263   */
     264  function getMissingDerivatives($update=false)
     265  {
     266    if ($this->data['size'] == 'original')
     267    {
     268      return array();
     269    }
     270   
     271    global $conf;
     272   
     273    $root_url = get_root_url();
     274    $uid = '&b='.time();
     275   
     276    $params = ImageStdParams::get_by_type($this->data['size']);
     277    $last_mod_time = $params->last_mod_time;
     278   
     279    $image_ids = array_keys($this->images);
     280    $to_update = $urls = $inserts = array();
     281   
     282    $conf['question_mark_in_urls'] = $conf['php_extension_in_urls'] = true;
     283    $conf['derivative_url_style'] = 2; //script
     284   
     285    // images which we need to update stats
     286    if ($update)
     287    {
     288      $query = '
     289SELECT image_id, filemtime FROM '.IMAGE_SIZES_TABLE.'
     290  WHERE image_id IN('.implode(',', $image_ids).')
     291    AND type = "'.$this->data['size'].'"
     292;';
     293      $registered = array_from_query($query, 'image_id', 'filemtime');
     294     
     295      $to_update = array_filter($registered, create_function('$t', 'return $t<'.$last_mod_time.';'));
     296      $to_update = array_merge($to_update, array_diff($image_ids, $registered));
     297    }
     298   
     299    $query = '
     300SELECT id, path, representative_ext, width, height, rotation
     301  FROM '.IMAGES_TABLE.'
     302  WHERE id IN('.implode(',', $image_ids).')
     303  ORDER BY id DESC
     304;';
     305
     306    $result = pwg_query($query);
     307    while ($row = pwg_db_fetch_assoc($result))
     308    {
     309      $src_image = new SrcImage($row);
     310      if ($src_image->is_mimetype()) continue;
     311     
     312      $derivative = new DerivativeImage($this->data['size'], $src_image);
     313      // if ($this->data['size'] != $derivative->get_type()) continue;
     314     
     315      $filemtime = @filemtime($derivative->get_path());
     316     
     317      if ($filemtime===false || $filemtime<$last_mod_time)
     318      {
     319        $urls[] = $root_url.$derivative->get_url().$uid;
     320      }
     321      else if ($update && in_array($row['id'], $to_update))
     322      {
     323        $imagesize = getimagesize($derivative->get_path());
     324       
     325        $inserts[ $row['id'] ] = array(
     326          'image_id' => $row['id'],
     327          'type' => $this->data['size'],
     328          'width' => $imagesize[0],
     329          'height' => $imagesize[1],
     330          'filesize' => filesize($derivative->get_path())/1024,
     331          'filemtime' => $filemtime,
     332          );
     333      }
     334    }
     335   
     336    if (!empty($inserts))
     337    {
     338      $query = '
     339DELETE FROM '.IMAGE_SIZES_TABLE.'
     340  WHERE image_id IN('.implode(',', array_keys($inserts)).')
     341;';
     342      pwg_query($query);
     343     
     344      mass_inserts(
     345        IMAGE_SIZES_TABLE,
     346        array('image_id','type','width','height','filesize'),
     347        $inserts
     348        );
     349    }
     350   
     351    return $urls;
     352  }
     353 
     354  /**
    257355   * deleteLastArchive
    258356   */
     
    311409      $query = '
    312410SELECT
    313     id,
    314     name,
    315     file,
    316     path,
    317     filesize
     411    id, name, file, path,
     412    representative_ext, rotation,
     413    filesize, width, height
    318414  FROM '.IMAGES_TABLE.'
    319415  WHERE id IN ('.implode(',', $images_to_add).')
    320416;';
    321417      $images_to_add = hash_from_query($query, 'id');
     418     
     419      if ($this->data['size'] != 'original')
     420      {
     421        $query = '
     422SELECT image_id, filesize
     423  FROM '.IMAGE_SIZES_TABLE.'
     424  WHERE image_id IN ('.implode(',', array_keys($images_to_add)).')
     425    AND type = "'.$this->data['size'].'"
     426;';
     427        $filesizes = simple_hash_from_query($query, 'image_id', 'filesize');
     428      }
    322429     
    323430      // open zip
     
    330437      $total_size = 0;
    331438      foreach ($images_to_add as $row)
    332       {       
    333         $zip->addFile(PHPWG_ROOT_PATH . $row['path'], $row['id'].'_'.stripslashes(get_filename_wo_extension($row['file'])).'.'.get_extension($row['path']));
     439      {
     440        if ($this->data['size'] == 'original')
     441        {
     442          $zip->addFile(PHPWG_ROOT_PATH . $row['path'], $row['id'].'_'.stripslashes(get_filename_wo_extension($row['file'])).'.'.get_extension($row['path']));
     443          $total_size+= $row['filesize'];
     444        }
     445        else
     446        {
     447          $src_image = new SrcImage($row);
     448          $derivative = new DerivativeImage($this->data['size'], $src_image);
     449          $path = $derivative->get_path();
     450     
     451          $zip->addFile($path, $row['id'].'_'.stripslashes(get_filename_wo_extension(basename($path))).'.'.get_extension($path));
     452          $total_size+= $filesizes[ $row['id'] ];
     453        }
    334454       
    335455        array_push($images_added, $row['id']);
    336456        $this->images[ $row['id'] ] = $this->data['last_zip'];
    337457       
    338         $total_size+= $row['filesize'];
    339458        if ($total_size >= $this->conf['max_size']*1024 and !$force_one_archive) break;
    340459      }
     
    370489     
    371490      // over estimed
    372       if ( $this->data['status'] == 'done' and $this->data['last_zip'] < $this->data['nb_zip'] )
     491      if ($this->data['status'] == 'done')
    373492      {
    374493        $this->updateParam('nb_zip', $this->data['last_zip']);
    375494      }
    376495      // under estimed
    377       else if ( $this->data['last_zip'] == $this->data['nb_zip'] and $this->data['status'] != 'done' )
     496      else if ($this->data['last_zip'] == $this->data['nb_zip'])
    378497      {
    379498        $this->updateParam('nb_zip', $this->data['last_zip']+1);
     
    400519    $image_ids = array_slice(array_keys($this->images), 0, $this->conf['max_elements']);
    401520   
    402     $query = '
     521    if ($this->data['size'] == 'original')
     522    {
     523      $query = '
    403524SELECT SUM(filesize) AS total
    404525  FROM '.IMAGES_TABLE.'
    405526  WHERE id IN ('.implode(',', $image_ids).')
    406527;';
     528    }
     529    else
     530    {
     531      $query = '
     532SELECT SUM(filesize) AS total
     533  FROM '.IMAGE_SIZES_TABLE.'
     534  WHERE image_id IN ('.implode(',', $image_ids).')
     535;';
     536    }
     537   
    407538    list($total) = pwg_db_fetch_row(pwg_query($query));
    408539    $this->data['estimated_total_size'] = $total;
     
    416547  function getEstimatedArchiveNumber()
    417548  {
    418     $nb_zip = ceil( $this->getEstimatedTotalSize() / ($this->conf['max_size']*1024) );
    419     $this->updateParam('nb_zip', $nb_zip);
    420     return $nb_zip;
     549    if ($this->data['status'] == 'done') return $this->data['nb_zip'];
     550   
     551    return ceil( $this->getEstimatedTotalSize() / ($this->conf['max_size']*1024) );
    421552  }
    422553 
     
    435566   
    436567    $out = '';
    437     for ($i=1; $i<=$this->data['nb_zip']; $i++)
     568    for ($i=1; $i<=$this->getEstimatedArchiveNumber(); $i++)
    438569    {
    439570      $out.= '<li id="zip-'.$i.'">';
     
    482613    $path.= $set['BASENAME'] . '_';
    483614    $path.= $this->data['user_id'] . $this->data['id'];
    484     $path.= $this->data['nb_zip']!=1 ? '_part' . $i : null;
     615    $path.= $this->getEstimatedArchiveNumber()!=1 ? '_part' . $i : null;
    485616    $path.= '.zip';
    486617   
     
    680811    $set = array(
    681812      'NB_IMAGES' =>     $this->data['nb_images'],
    682       'NB_ARCHIVES' =>   $this->data['nb_zip'],
     813      'NB_ARCHIVES' =>   $this->data['status']=='new' ? l10n('unknown') : $this->getEstimatedArchiveNumber(),
    683814      'STATUS' =>        $this->data['status'],
    684815      'LAST_ZIP' =>      $this->data['last_zip'],
    685       'TOTAL_SIZE' =>    ceil($this->getEstimatedTotalSize()/1024),
    686       'LINKS' =>         $this->getDownloadList(BATCH_DOWNLOAD_PUBLIC . 'init_zip'),
     816      'TOTAL_SIZE' =>    $this->data['status']=='new' ? l10n('unknown') : ceil($this->getEstimatedTotalSize()/1024),
     817      // 'LINKS' =>         $this->getDownloadList(BATCH_DOWNLOAD_PUBLIC . 'init_zip'),
    687818      'DATE_CREATION' => format_date($this->data['date_creation'], true),
     819      'SIZE_ID' =>       $this->data['size'],
     820      'SIZE' =>          l10n($this->data['size']),
    688821      );
     822     
     823    if ($this->data['size'] != 'original')
     824    {
     825      $params = ImageStdParams::get_by_type($this->data['size']);
     826      $set['SIZE_INFO'] = $params->sizing->ideal_size[0].' x '.$params->sizing->ideal_size[1];
     827    }
    689828   
    690829    return array_merge($set, $this->getNames());
Note: See TracChangeset for help on using the changeset viewer.