Ejemplo n.º 1
0
 /**
  *
  * @param type $ThisFileInfoIndex
  * @param type $filename
  * @param type $offset
  * @param type $length
  * @return boolean
  * @throws Exception
  */
 public function saveAttachment(&$ThisFileInfoIndex, $filename, $offset, $length)
 {
     try {
         if (!GetId3_Lib_Helper::intValueSupported($offset + $length)) {
             throw new Exception('it extends beyond the ' . round(PHP_INT_MAX / 1073741824) . 'GB limit');
         }
         // do not extract at all
         if ($this->getid3->option_save_attachments === self::ATTACHMENTS_NONE) {
             unset($ThisFileInfoIndex);
             // do not set any
         } else {
             if ($this->getid3->option_save_attachments === self::ATTACHMENTS_INLINE) {
                 // get whole data in one pass, till it is anyway stored in memory
                 $this->fseek($offset);
                 $ThisFileInfoIndex = $this->fread($length);
                 if ($ThisFileInfoIndex === false || strlen($ThisFileInfoIndex) != $length) {
                     // verify
                     throw new Exception('failed to read attachment data');
                 }
             } else {
                 // set up destination path
                 $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->getid3->option_save_attachments), DIRECTORY_SEPARATOR);
                 if (!is_dir($dir) || !is_writable($dir)) {
                     // check supplied directory
                     throw new Exception('supplied path (' . $dir . ') does not exist, or is not writable');
                 }
                 $dest = $dir . DIRECTORY_SEPARATOR . $filename;
                 // create dest file
                 if (($fp_dest = fopen($dest, 'wb')) == false) {
                     throw new Exception('failed to create file ' . $dest);
                 }
                 // copy data
                 $this->fseek($offset);
                 $buffersize = $this->data_string_flag ? $length : $this->getid3->fread_buffer_size();
                 $bytesleft = $length;
                 while ($bytesleft > 0) {
                     if (($buffer = $this->fread(min($buffersize, $bytesleft))) === false || ($byteswritten = fwrite($fp_dest, $buffer)) === false) {
                         fclose($fp_dest);
                         unlink($dest);
                         throw new Exception($buffer === false ? 'not enough data to read' : 'failed to write to destination file, may be not enough disk space');
                     }
                     $bytesleft -= $byteswritten;
                 }
                 fclose($fp_dest);
                 $ThisFileInfoIndex = $dest;
             }
         }
     } catch (Exception $e) {
         unset($ThisFileInfoIndex);
         // do not set any is case of error
         $this->warning('Failed to extract attachment ' . $filename . ': ' . $e->getMessage());
         return false;
     }
     return true;
 }