getid3_handler::saveAttachment() –

You appear to be a bot. Output may be restricted

Description

Usage

$string|null = getid3_handler::saveAttachment( $name, $offset, $length, $image_mime );

Parameters

$name
( string ) required
$offset
( int ) required
$length
( int ) required
$image_mime
( string ) optional

Returns

string|null

Source

File name: wordpress/wp-includes/ID3/getid3.php
Lines:

1 to 72 of 72
  public function saveAttachment($name, $offset, $length, $image_mime=null) {
    $fp_dest = null;
    $dest = null;
    try {

      // do not extract at all
      if ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_NONE) {

        $attachment = null; // do not set any

      // extract to return array
      } elseif ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_INLINE) {

        $this->getid3_handler::fseek($offset);
        $attachment = $this->getid3_handler::fread($length); // get whole data in one pass, till it is anyway stored in memory
        if ($attachment === false || strlen($attachment) != $length) {
          throw new Exception('failed to read attachment data');
        }

      // assume directory path is given
      } else {

        // set up destination path
        $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->getid3->option_save_attachments), DIRECTORY_SEPARATOR);
        if (!is_dir($dir) || !getID3::is_writable($dir)) { // check supplied directory
          throw new Exception('supplied path ('.$dir.') does not exist, or is not writable');
        }
        $dest = $dir.DIRECTORY_SEPARATOR.$name.($image_mime ? '.'.getid3_lib::ImageExtFromMime($image_mime) : '');

        // create dest file
        if (($fp_dest = fopen($dest, 'wb')) == false) {
          throw new Exception('failed to create file '.$dest);
        }

        // copy data
        $this->getid3_handler::fseek($offset);
        $buffersize = ($this->data_string_flag ? $length : $this->getid3->fread_buffer_size());
        $bytesleft = $length;
        while ($bytesleft > 0) {
          if (($buffer = $this->getid3_handler::fread(min($buffersize, $bytesleft))) === false || ($byteswritten = fwrite($fp_dest, $buffer)) === false || ($byteswritten === 0)) {
            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);
        $attachment = $dest;

      }

    } catch (Exception $e) {

      // close and remove dest file if created
      if (isset($fp_dest) && is_resource($fp_dest)) {
        fclose($fp_dest);
      }

      if (isset($dest) && file_exists($dest)) {
        unlink($dest);
      }

      // do not set any is case of error
      $attachment = null;
      $this->getid3_handler::warning('Failed to extract attachment '.$name.': '.$e->getMessage());

    }

    // seek to the end of attachment
    $this->getid3_handler::fseek($offset + $length);

    return $attachment;
  }
 

 View on GitHub View on Trac