<commit_before><?php

/**
 * Multimedia
 *
 * This class has been auto-generated by the Doctrine ORM Framework
 *
 * @package    darwin
 * @subpackage model
 * @author     DB team <<EMAIL>>
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class Multimedia extends BaseMultimedia
{
  public static $allowed_mime_type = array(
    'csv' => 'text/csv',
    'txt' => 'text/plain',
//   'htm' => 'text/html',
    'html' => 'text/html',
//    'php' => 'text/html',
    'css' => 'text/css',
//    'js' => 'application/javascript',
//    'json' => 'application/json',
    'xml' => 'application/xml',
    'xsd' => 'application/xsd',
//    'swf' => 'application/x-shockwave-flash',
//    'flv' => 'video/x-flv',

    // images
    'png' => 'image/png',
//    'jpe' => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'jpg' => 'image/jpeg',
    'gif' => 'image/gif',
    'bmp' => 'image/bmp',
    'ico' => 'image/vnd.microsoft.icon',
    'tiff' => 'image/tiff',
    'tif' => 'image/tiff',
    'svg' => 'image/svg+xml',
    'ogg' => 'application/ogg',
//    'svgz' => 'image/svg+xml',

    // archives
    'zip' => 'application/zip',
    'rar' => 'application/x-rar-compressed',
//    'exe' => 'application/x-msdownload',
//    'msi' => 'application/x-msdownload',
//    'cab' => 'application/vnd.ms-cab-compressed',
    // audio
    'wav' => 'audio/x-wav',
    'mp3' => 'audio/mpeg',
    'wma' => 'audio/x-ms-wma',
    'rla' => 'audio/vnd.rn-realaudio',
    'flac' => 'audio/flac',
    'aac' => 'audio/aac',
    // video
//    'qt' => 'video/quicktime',
    'mov' => 'video/quicktime',
    'mp4' => 'video/mp4',
    'mpeg' => 'video/mpeg',

    // adobe
    'pdf' => 'application/pdf',
    'psd' => 'image/vnd.adobe.photoshop',
    'ai' => 'application/postscript',
    'eps' => 'application/postscript',
    'ps' => 'application/postscript',

    // ms office
    'doc' => 'application/msword',
    'rtf' => 'application/rtf',
    'xls' => 'application/vnd.ms-excel',
    'ppt' => 'application/vnd.ms-powerpoint',

    // open office
    'odt' => 'application/vnd.oasis.opendocument.text',
    'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        );

  public function getCreationDateMasked()
  {
    $dateTime = new FuzzyDateTime($this->_get('creation_date'), $this->_get('creation_date_mask'),false,false);
    return $dateTime->getDateMasked();
  }

  public function getCreationDate()
  {
    $date = new FuzzyDateTime($this->_get('creation_date'),$this->_get('creation_date_mask'),false, false);
    return $date->getDateTime(null, 'Y/m/d', null);
  }

  public function changeUri()
  {
    $this->checkUploadPathAvailable() ;
    rename(
      sfConfig::get('sf_upload_dir')."/multimedia/temp/".$this->_get('uri'),
      sfConfig::get('sf_upload_dir')."/multimedia/".$this->getBuildedUrl()
    );
    $this->setUri($this->getBuildedUrl()) ;
  }

  public function getBuildedUrl()
  {
    return $this->getBuildedDir().'/'. $this->_get('uri');
  }
  public function getBuildedDir()
  {
    //Make something like multimedia/00/01/01/12  for the multimed of id= 10112
    $num = sprintf('%08d', $this->getRecordId());
    return $this->getReferencedRelation().'/'.implode('/',str_split($num,'2'));
  }

  public function move($from){
    $this->checkUploadPathAvailable() ;
    $filename = basename($from);
    $this->setUri($this->getBuildedDir().$filename);
    copy(
      sfConfig::get('sf_upload_dir')."/multimedia/".$from,
      sfConfig::get('sf_upload_dir')."/multimedia/".$this->getUri()
    );
  }

  protected function checkUploadPathAvailable()
  {
    //function used to verify if the folder for the uploaded file exists
    $path = sfConfig::get('sf_upload_dir')."/multimedia/".$this->getBuildedDir();
    if(!is_dir($path)) mkdir($path,0750,true) ;
    return true;
  }

  public static function getMimeTypeFor($key)
  {
    return self::$allowed_mime_type[$key] ;
  }

  public static function CheckMimeType($mime_type)
  {
    return(in_array($mime_type,self::$allowed_mime_type)?true:false);
  }

  public function getFullURI()
  {
    return sfConfig::get('sf_upload_dir').'/multimedia/'.$this->getUri();
  }

  public function getSize()
  {
    return filesize($this->getFullURI());
  }

  public function hasPreview()
  {
    return self::canBePreviewed($this->getMimeType());
  }

  public static function canBePreviewed($mime) {
    if(in_array($mime ,array('image/png', 'image/jpeg', 'application/pdf') ) )
      return true;
    return false;
  }

  public function getPreview($new_w = 200, $new_h = 200)
  {
    if($this->hasPreview())
    {
      $image = new Imagick($this->getFullURI());
      if($this->getMimeType() == 'application/pdf') {
        // Display page 1 of pdfs
        $image->setIteratorIndex(0);
      }
      $image->thumbnailImage( $new_w, $new_h, true );
      $image->setImageFormat( "png" );
      $image->setCompression(Imagick::COMPRESSION_LZW);
      $image->setCompressionQuality(90);
      return $image->getImageBlob();
    }
  }

  public function getHumanSize()
  {

    return $this->HumanReadableFilesize($this->getSize());
  }

  public static function HumanReadableFilesize($size) {
    $mod = 1024;
    $units = explode(' ','B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }<commit_msg>Added file_exists check avoiding getting a fatal error calling a file that does not exist anymore<commit_after><?php

/**
 * Multimedia
 *
 * This class has been auto-generated by the Doctrine ORM Framework
 *
 * @package    darwin
 * @subpackage model
 * @author     DB team <<EMAIL>>
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class Multimedia extends BaseMultimedia
{
  public static $allowed_mime_type = array(
    'csv' => 'text/csv',
    'txt' => 'text/plain',
//   'htm' => 'text/html',
    'html' => 'text/html',
//    'php' => 'text/html',
    'css' => 'text/css',
//    'js' => 'application/javascript',
//    'json' => 'application/json',
    'xml' => 'application/xml',
    'xsd' => 'application/xsd',
//    'swf' => 'application/x-shockwave-flash',
//    'flv' => 'video/x-flv',

    // images
    'png' => 'image/png',
//    'jpe' => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'jpg' => 'image/jpeg',
    'gif' => 'image/gif',
    'bmp' => 'image/bmp',
    'ico' => 'image/vnd.microsoft.icon',
    'tiff' => 'image/tiff',
    'tif' => 'image/tiff',
    'svg' => 'image/svg+xml',
    'ogg' => 'application/ogg',
//    'svgz' => 'image/svg+xml',

    // archives
    'zip' => 'application/zip',
    'rar' => 'application/x-rar-compressed',
//    'exe' => 'application/x-msdownload',
//    'msi' => 'application/x-msdownload',
//    'cab' => 'application/vnd.ms-cab-compressed',
    // audio
    'wav' => 'audio/x-wav',
    'mp3' => 'audio/mpeg',
    'wma' => 'audio/x-ms-wma',
    'rla' => 'audio/vnd.rn-realaudio',
    'flac' => 'audio/flac',
    'aac' => 'audio/aac',
    // video
//    'qt' => 'video/quicktime',
    'mov' => 'video/quicktime',
    'mp4' => 'video/mp4',
    'mpeg' => 'video/mpeg',

    // adobe
    'pdf' => 'application/pdf',
    'psd' => 'image/vnd.adobe.photoshop',
    'ai' => 'application/postscript',
    'eps' => 'application/postscript',
    'ps' => 'application/postscript',

    // ms office
    'doc' => 'application/msword',
    'rtf' => 'application/rtf',
    'xls' => 'application/vnd.ms-excel',
    'ppt' => 'application/vnd.ms-powerpoint',

    // open office
    'odt' => 'application/vnd.oasis.opendocument.text',
    'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        );

  public function getCreationDateMasked()
  {
    $dateTime = new FuzzyDateTime($this->_get('creation_date'), $this->_get('creation_date_mask'),false,false);
    return $dateTime->getDateMasked();
  }

  public function getCreationDate()
  {
    $date = new FuzzyDateTime($this->_get('creation_date'),$this->_get('creation_date_mask'),false, false);
    return $date->getDateTime(null, 'Y/m/d', null);
  }

  public function changeUri()
  {
    $this->checkUploadPathAvailable() ;
    rename(
      sfConfig::get('sf_upload_dir')."/multimedia/temp/".$this->_get('uri'),
      sfConfig::get('sf_upload_dir')."/multimedia/".$this->getBuildedUrl()
    );
    $this->setUri($this->getBuildedUrl()) ;
  }

  public function getBuildedUrl()
  {
    return $this->getBuildedDir().'/'. $this->_get('uri');
  }
  public function getBuildedDir()
  {
    //Make something like multimedia/00/01/01/12  for the multimed of id= 10112
    $num = sprintf('%08d', $this->getRecordId());
    return $this->getReferencedRelation().'/'.implode('/',str_split($num,'2'));
  }

  public function move($from){
    $this->checkUploadPathAvailable() ;
    $filename = basename($from);
    $this->setUri($this->getBuildedDir().$filename);
    copy(
      sfConfig::get('sf_upload_dir')."/multimedia/".$from,
      sfConfig::get('sf_upload_dir')."/multimedia/".$this->getUri()
    );
  }

  protected function checkUploadPathAvailable()
  {
    //function used to verify if the folder for the uploaded file exists
    $path = sfConfig::get('sf_upload_dir')."/multimedia/".$this->getBuildedDir();
    if(!is_dir($path)) mkdir($path,0750,true) ;
    return true;
  }

  public static function getMimeTypeFor($key)
  {
    return self::$allowed_mime_type[$key] ;
  }

  public static function CheckMimeType($mime_type)
  {
    return(in_array($mime_type,self::$allowed_mime_type)?true:false);
  }

  public function getFullURI()
  {
    return sfConfig::get('sf_upload_dir').'/multimedia/'.$this->getUri();
  }

  public function getSize()
  {
    if(file_exists($this->getFullURI())) {
      return filesize($this->getFullURI());
    }
    return 0;
  }

  public function hasPreview()
  {
    return self::canBePreviewed($this->getMimeType());
  }

  public static function canBePreviewed($mime) {
    if(in_array($mime ,array('image/png', 'image/jpeg', 'application/pdf') ) )
      return true;
    return false;
  }

  public function getPreview($new_w = 200, $new_h = 200)
  {
    if($this->hasPreview() && file_exists($this->getFullURI()))
    {
      $image = new Imagick($this->getFullURI());
      if($this->getMimeType() == 'application/pdf') {
        // Display page 1 of pdfs
        $image->setIteratorIndex(0);
      }
      $image->thumbnailImage( $new_w, $new_h, true );
      $image->setImageFormat( "png" );
      $image->setCompression(Imagick::COMPRESSION_LZW);
      $image->setCompressionQuality(90);
      return $image->getImageBlob();
    }
  }

  public function getHumanSize()
  {

    return $this->HumanReadableFilesize($this->getSize());
  }

  public static function HumanReadableFilesize($size) {
    $mod = 1024;
    $units = explode(' ','B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }
{'fraction_non_alphanumeric': 0.19748636909712597, 'fraction_numerical': 0.010442657795028187, 'mean_word_length': 3.186460348162476, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 179, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3505519', 'n_tokens_mistral': 4013, 'n_tokens_neox': 3790, 'n_words': 661}