2011-01-10 19:24:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skeleton subclass for representing a row from the 'cc_files' table.
|
|
|
|
*
|
2012-02-07 13:29:50 +01:00
|
|
|
*
|
2011-01-10 19:24:21 +01:00
|
|
|
*
|
|
|
|
* You should add additional methods to this class to meet the
|
|
|
|
* application requirements. This class will only be generated as
|
|
|
|
* long as it does not already exist in the output directory.
|
|
|
|
*
|
|
|
|
* @package propel.generator.campcaster
|
|
|
|
*/
|
2015-02-20 20:27:16 +01:00
|
|
|
|
|
|
|
class InvalidMetadataException extends Exception
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileNotFoundException extends Exception
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class OverDiskQuotaException extends Exception
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-10 19:24:21 +01:00
|
|
|
class CcFiles extends BaseCcFiles {
|
2015-02-20 20:27:16 +01:00
|
|
|
|
|
|
|
const MUSIC_DIRS_STOR_PK = 1;
|
|
|
|
|
2015-02-20 22:36:36 +01:00
|
|
|
const IMPORT_STATUS_SUCCESS = 0;
|
|
|
|
const IMPORT_STATUS_PENDING = 1;
|
|
|
|
const IMPORT_STATUS_FAILED = 2;
|
|
|
|
|
2015-02-20 20:27:16 +01:00
|
|
|
|
|
|
|
//fields that are not modifiable via our RESTful API
|
|
|
|
private static $blackList = array(
|
|
|
|
'id',
|
|
|
|
'directory',
|
|
|
|
'filepath',
|
|
|
|
'file_exists',
|
|
|
|
'mtime',
|
|
|
|
'utime',
|
|
|
|
'lptime',
|
|
|
|
'silan_check',
|
|
|
|
'soundcloud_id',
|
|
|
|
'is_scheduled',
|
|
|
|
'is_playlist'
|
|
|
|
);
|
|
|
|
|
2014-07-15 22:32:48 +02:00
|
|
|
//fields we should never expose through our RESTful API
|
|
|
|
private static $privateFields = array(
|
2015-02-20 20:27:16 +01:00
|
|
|
'file_exists',
|
|
|
|
'silan_check',
|
|
|
|
'is_scheduled',
|
|
|
|
'is_playlist'
|
2014-07-15 22:32:48 +02:00
|
|
|
);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a sanitized version of the file metadata, suitable for public access.
|
|
|
|
* @param $fileId
|
|
|
|
*/
|
|
|
|
public static function getSanitizedFileById($fileId)
|
|
|
|
{
|
|
|
|
$file = CcFilesQuery::create()->findPk($fileId);
|
|
|
|
if ($file) {
|
|
|
|
return CcFiles::sanitizeResponse($file);
|
|
|
|
} else {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Used to create a CcFiles object from an array containing metadata and a file uploaded by POST.
|
|
|
|
* This is used by our Media REST API!
|
|
|
|
* @param $fileArray An array containing metadata for a CcFiles object.
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function createFromUpload($fileArray)
|
|
|
|
{
|
|
|
|
if (Application_Model_Systemstatus::isDiskOverQuota()) {
|
|
|
|
throw new OverDiskQuotaException();
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:36:36 +01:00
|
|
|
/* If full_path is set, the post request came from ftp.
|
|
|
|
* Users are allowed to upload folders via ftp. If this is the case
|
|
|
|
* we need to include the folder name with the file name, otherwise
|
|
|
|
* files won't get removed from the organize folder.
|
|
|
|
*/
|
|
|
|
|
2015-03-06 17:06:17 +01:00
|
|
|
//Extract the original filename, which we set as the temporary title for the track
|
|
|
|
//until it's finished being processed by the analyzer.
|
|
|
|
$originalFilename = $_FILES["file"]["name"];
|
2015-02-20 22:36:36 +01:00
|
|
|
$tempFilePath = $_FILES['file']['tmp_name'];
|
|
|
|
|
|
|
|
try {
|
|
|
|
self::createAndImport($fileArray, $tempFilePath, $originalFilename);
|
|
|
|
} catch (Exception $e)
|
|
|
|
{
|
|
|
|
@unlink($tempFilePath);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Import a music file to the library from a local file on disk (something pre-existing).
|
|
|
|
* This function allows you to copy a file rather than move it, which is useful for importing
|
|
|
|
* static music files (like sample tracks).
|
|
|
|
* @param string $filePath The full path to the audio file to import.
|
|
|
|
* @param bool $copyFile True if you want to just copy the false, false if you want to move it (default false)
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-02-20 23:34:58 +01:00
|
|
|
public static function createFromLocalFile($fileArray, $filePath, $copyFile=false)
|
2015-02-20 22:36:36 +01:00
|
|
|
{
|
|
|
|
$info = pathinfo($filePath);
|
|
|
|
$fileName = basename($filePath).'.'.$info['extension'];
|
|
|
|
self::createAndImport($fileArray, $filePath, $fileName, $copyFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a new CcFiles object/row and import a file for it.
|
|
|
|
* You shouldn't call this directly. Either use createFromUpload() or createFromLocalFile().
|
|
|
|
* @param array $fileArray Any metadata to pre-fill for the audio file
|
|
|
|
* @param string $filePath The full path to the audio file to import
|
|
|
|
* @param string $originalFilename
|
|
|
|
* @param bool $copyFile
|
|
|
|
* @return mixed
|
|
|
|
* @throws Exception
|
|
|
|
* @throws PropelException
|
|
|
|
*/
|
|
|
|
private static function createAndImport($fileArray, $filePath, $originalFilename, $copyFile=false)
|
|
|
|
{
|
2015-02-20 20:27:16 +01:00
|
|
|
$file = new CcFiles();
|
|
|
|
|
2015-02-20 22:36:36 +01:00
|
|
|
try
|
|
|
|
{
|
2015-02-20 20:27:16 +01:00
|
|
|
$fileArray = self::removeBlacklistedFields($fileArray);
|
|
|
|
|
|
|
|
self::validateFileArray($fileArray);
|
|
|
|
|
|
|
|
$file->fromArray($fileArray);
|
|
|
|
$file->setDbOwnerId(self::getOwnerId());
|
|
|
|
$now = new DateTime("now", new DateTimeZone("UTC"));
|
2015-02-20 22:36:36 +01:00
|
|
|
$file->setDbTrackTitle($originalFilename);
|
2015-02-20 20:27:16 +01:00
|
|
|
$file->setDbUtime($now);
|
|
|
|
$file->setDbHidden(true);
|
|
|
|
$file->save();
|
|
|
|
|
2015-02-20 22:36:36 +01:00
|
|
|
//Only accept files with a file extension that we support.
|
|
|
|
$fileExtension = pathinfo($originalFilename, PATHINFO_EXTENSION);
|
|
|
|
if (!in_array(strtolower($fileExtension), explode(",", "ogg,mp3,oga,flac,wav,m4a,mp4,opus"))) {
|
|
|
|
throw new Exception("Bad file extension.");
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:09:08 +01:00
|
|
|
$callbackUrl = Application_Common_HTTPHelper::getStationUrl() . "/rest/media/" . $file->getPrimaryKey();
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-20 22:36:36 +01:00
|
|
|
Application_Service_MediaService::importFileToLibrary($callbackUrl, $filePath,
|
|
|
|
$originalFilename, self::getOwnerId(), $copyFile);
|
|
|
|
|
2015-02-20 20:27:16 +01:00
|
|
|
return CcFiles::sanitizeResponse($file);
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
2015-02-20 22:36:36 +01:00
|
|
|
$file->setDbImportStatus(self::IMPORT_STATUS_FAILED);
|
2015-02-20 20:27:16 +01:00
|
|
|
$file->setDbHidden(true);
|
2015-02-20 22:36:36 +01:00
|
|
|
$file->save();
|
2015-02-20 20:27:16 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Update a file with metadata specified in an array.
|
2015-02-20 22:36:36 +01:00
|
|
|
* @param $fileId string The ID of the file to update in the DB.
|
|
|
|
* @param $fileArray array An associative array containing metadata. Replaces those fields if they exist.
|
2015-02-20 20:27:16 +01:00
|
|
|
* @return array A sanitized version of the file metadata array.
|
|
|
|
* @throws Exception
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
* @throws PropelException
|
|
|
|
*/
|
|
|
|
public static function updateFromArray($fileId, $fileArray)
|
|
|
|
{
|
|
|
|
$file = CcFilesQuery::create()->findPk($fileId);
|
|
|
|
|
|
|
|
$fileArray = self::removeBlacklistedFields($fileArray);
|
|
|
|
$fileArray = self::stripTimeStampFromYearTag($fileArray);
|
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
try {
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
self::validateFileArray($fileArray);
|
|
|
|
if ($file && isset($fileArray["resource_id"])) {
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
$file->fromArray($fileArray, BasePeer::TYPE_FIELDNAME);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
//store the original filename
|
|
|
|
$file->setDbFilepath($fileArray["filename"]);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
$fileSizeBytes = $fileArray["filesize"];
|
|
|
|
if (!isset($fileSizeBytes) || $fileSizeBytes === false) {
|
|
|
|
throw new FileNotFoundException("Invalid filesize for $fileId");
|
|
|
|
}
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
$cloudFile = new CloudFile();
|
|
|
|
$cloudFile->setStorageBackend($fileArray["storage_backend"]);
|
|
|
|
$cloudFile->setResourceId($fileArray["resource_id"]);
|
|
|
|
$cloudFile->setCcFiles($file);
|
|
|
|
$cloudFile->save();
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
Application_Model_Preference::updateDiskUsage($fileSizeBytes);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
$now = new DateTime("now", new DateTimeZone("UTC"));
|
|
|
|
$file->setDbMtime($now);
|
|
|
|
$file->save();
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
} else if ($file) {
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
// Since we check for this value when deleting files, set it first
|
|
|
|
$file->setDbDirectory(self::MUSIC_DIRS_STOR_PK);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
$file->fromArray($fileArray, BasePeer::TYPE_FIELDNAME);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
//Our RESTful API takes "full_path" as a field, which we then split and translate to match
|
|
|
|
//our internal schema. Internally, file path is stored relative to a directory, with the directory
|
|
|
|
//as a foreign key to cc_music_dirs.
|
|
|
|
if (isset($fileArray["full_path"])) {
|
|
|
|
$fileSizeBytes = filesize($fileArray["full_path"]);
|
|
|
|
if (!isset($fileSizeBytes) || $fileSizeBytes === false) {
|
|
|
|
throw new FileNotFoundException("Invalid filesize for $fileId");
|
|
|
|
}
|
|
|
|
Application_Model_Preference::updateDiskUsage($fileSizeBytes);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
$fullPath = $fileArray["full_path"];
|
|
|
|
$storDir = Application_Model_MusicDir::getStorDir()->getDirectory();
|
|
|
|
$pos = strpos($fullPath, $storDir);
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
if ($pos !== FALSE) {
|
|
|
|
assert($pos == 0); //Path must start with the stor directory path
|
|
|
|
|
|
|
|
$filePathRelativeToStor = substr($fullPath, strlen($storDir));
|
|
|
|
$file->setDbFilepath($filePathRelativeToStor);
|
|
|
|
}
|
2015-02-20 20:27:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
$now = new DateTime("now", new DateTimeZone("UTC"));
|
|
|
|
$file->setDbMtime($now);
|
|
|
|
$file->save();
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2015-02-26 20:26:33 +01:00
|
|
|
} else {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException $e)
|
|
|
|
{
|
|
|
|
$file->setDbImportStatus(self::IMPORT_STATUS_FAILED);
|
|
|
|
$file->setDbHidden(true);
|
|
|
|
$file->save();
|
|
|
|
throw $e;
|
2015-02-20 20:27:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return CcFiles::sanitizeResponse($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Delete a file from the database and disk (or cloud).
|
|
|
|
* @param $id The file ID
|
|
|
|
* @throws DeleteScheduledFileException
|
|
|
|
* @throws Exception
|
|
|
|
* @throws FileNoPermissionException
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
* @throws PropelException
|
|
|
|
*/
|
|
|
|
public static function deleteById($id)
|
|
|
|
{
|
|
|
|
$file = CcFilesQuery::create()->findPk($id);
|
|
|
|
if ($file) {
|
|
|
|
$con = Propel::getConnection();
|
2015-03-03 18:52:41 +01:00
|
|
|
$storedFile = Application_Model_StoredFile::RecallById($id, $con);
|
|
|
|
$storedFile->delete();
|
2015-02-20 20:27:16 +01:00
|
|
|
} else {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static function validateFileArray(&$fileArray)
|
|
|
|
{
|
|
|
|
// Sanitize any wildly incorrect metadata before it goes to be validated
|
|
|
|
FileDataHelper::sanitizeData($fileArray);
|
|
|
|
|
|
|
|
// EditAudioMD form is used here for validation
|
|
|
|
$fileForm = new Application_Form_EditAudioMD();
|
|
|
|
$fileForm->startForm(0); //The file ID doesn't matter here
|
|
|
|
$fileForm->populate($fileArray);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we are truncating metadata of any characters greater than the
|
|
|
|
* max string length set in the database. In the rare case a track's
|
|
|
|
* genre is more than 64 chars, for example, we don't want to reject
|
|
|
|
* tracks for that reason
|
|
|
|
*/
|
|
|
|
foreach($fileArray as $tag => &$value) {
|
|
|
|
if ($fileForm->getElement($tag)) {
|
|
|
|
$stringLengthValidator = $fileForm->getElement($tag)->getValidator('StringLength');
|
|
|
|
//$stringLengthValidator will be false if the StringLength validator doesn't exist on the current element
|
|
|
|
//in which case we don't have to truncate the extra characters
|
|
|
|
if ($stringLengthValidator) {
|
|
|
|
$value = substr($value, 0, $stringLengthValidator->getMax());
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = self::stripInvalidUtf8Characters($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$fileForm->isValidPartial($fileArray)) {
|
|
|
|
$errors = $fileForm->getErrors();
|
|
|
|
$messages = $fileForm->getMessages();
|
|
|
|
Logging::error($messages);
|
|
|
|
throw new Exception("Data validation failed: $errors - $messages");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getCueLength()
|
2013-05-08 20:19:22 +02:00
|
|
|
{
|
|
|
|
$cuein = $this->getDbCuein();
|
|
|
|
$cueout = $this->getDbCueout();
|
|
|
|
|
|
|
|
$cueinSec = Application_Common_DateHelper::calculateLengthInSeconds($cuein);
|
|
|
|
$cueoutSec = Application_Common_DateHelper::calculateLengthInSeconds($cueout);
|
|
|
|
$lengthSec = bcsub($cueoutSec, $cueinSec, 6);
|
|
|
|
|
|
|
|
$length = Application_Common_DateHelper::secondsToPlaylistTime($lengthSec);
|
|
|
|
|
|
|
|
return $length;
|
|
|
|
}
|
2011-01-10 19:24:21 +01:00
|
|
|
|
2013-05-29 19:47:55 +02:00
|
|
|
public function setDbTrackNumber($v)
|
|
|
|
{
|
|
|
|
$max = pow(2, 31)-1;
|
|
|
|
$v = ($v > $max) ? $max : $v;
|
|
|
|
|
|
|
|
return parent::setDbTrackNumber($v);
|
|
|
|
}
|
|
|
|
|
2012-11-05 16:08:29 +01:00
|
|
|
// returns true if the file exists and is not hidden
|
2012-11-05 16:57:18 +01:00
|
|
|
public function visible() {
|
2012-11-05 16:08:29 +01:00
|
|
|
return $this->getDbFileExists() && !$this->getDbHidden();
|
|
|
|
}
|
|
|
|
|
2012-09-19 16:23:54 +02:00
|
|
|
public function reassignTo($user)
|
|
|
|
{
|
2012-08-28 22:22:35 +02:00
|
|
|
$this->setDbOwnerId( $user->getDbId() );
|
2012-09-19 00:20:33 +02:00
|
|
|
$this->save();
|
2012-08-28 22:22:35 +02:00
|
|
|
}
|
2011-01-10 19:24:21 +01:00
|
|
|
|
2014-07-15 22:32:48 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Strips out the private fields we do not want to send back in API responses
|
2015-02-20 22:36:36 +01:00
|
|
|
* @param $file string a CcFiles object
|
2014-07-15 22:32:48 +02:00
|
|
|
*/
|
|
|
|
//TODO: rename this function?
|
|
|
|
public static function sanitizeResponse($file)
|
|
|
|
{
|
|
|
|
$response = $file->toArray(BasePeer::TYPE_FIELDNAME);
|
|
|
|
|
|
|
|
foreach (self::$privateFields as $key) {
|
|
|
|
unset($response[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2014-10-30 17:12:47 +01:00
|
|
|
/**
|
|
|
|
* Returns the file size in bytes.
|
|
|
|
*/
|
2014-07-24 22:56:15 +02:00
|
|
|
public function getFileSize()
|
|
|
|
{
|
2015-02-17 20:51:51 +01:00
|
|
|
return $this->getDbFilesize();
|
2014-07-24 22:56:15 +02:00
|
|
|
}
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2014-07-24 22:56:15 +02:00
|
|
|
public function getFilename()
|
|
|
|
{
|
|
|
|
$info = pathinfo($this->getAbsoluteFilePath());
|
2015-02-26 17:29:08 +01:00
|
|
|
//filename doesn't contain the extension because PHP is awful
|
|
|
|
return $info['filename'].".".$info['extension'];
|
2014-07-24 22:56:15 +02:00
|
|
|
}
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2014-10-30 17:12:47 +01:00
|
|
|
/**
|
|
|
|
* Returns the file's absolute file path stored on disk.
|
|
|
|
*/
|
2014-10-22 20:17:44 +02:00
|
|
|
public function getURLForTrackPreviewOrDownload()
|
|
|
|
{
|
|
|
|
return $this->getAbsoluteFilePath();
|
|
|
|
}
|
2015-02-20 20:27:16 +01:00
|
|
|
|
2014-10-30 17:12:47 +01:00
|
|
|
/**
|
|
|
|
* Returns the file's absolute file path stored on disk.
|
|
|
|
*/
|
2014-07-24 22:56:15 +02:00
|
|
|
public function getAbsoluteFilePath()
|
|
|
|
{
|
2014-07-28 22:02:06 +02:00
|
|
|
$music_dir = Application_Model_MusicDir::getDirByPK($this->getDbDirectory());
|
2014-07-24 22:56:15 +02:00
|
|
|
if (!$music_dir) {
|
2015-02-26 19:52:51 +01:00
|
|
|
throw new Exception("Invalid music_dir for file " . $this->getDbId() . " in database.");
|
2014-07-24 22:56:15 +02:00
|
|
|
}
|
|
|
|
$directory = $music_dir->getDirectory();
|
2014-11-10 18:53:07 +01:00
|
|
|
$filepath = $this->getDbFilepath();
|
2014-07-24 22:56:15 +02:00
|
|
|
return Application_Common_OsPath::join($directory, $filepath);
|
|
|
|
}
|
2015-02-20 20:27:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Strips out fields from incoming request data that should never be modified
|
|
|
|
* from outside of Airtime
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
private static function removeBlacklistedFields($data)
|
|
|
|
{
|
|
|
|
foreach (self::$blackList as $key) {
|
|
|
|
unset($data[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static function getOwnerId()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (Zend_Auth::getInstance()->hasIdentity()) {
|
|
|
|
$service_user = new Application_Service_UserService();
|
|
|
|
return $service_user->getCurrentUser()->getDbId();
|
|
|
|
} else {
|
|
|
|
$defaultOwner = CcSubjsQuery::create()
|
|
|
|
->filterByDbType('A')
|
|
|
|
->orderByDbId()
|
|
|
|
->findOne();
|
|
|
|
if (!$defaultOwner) {
|
|
|
|
// what to do if there is no admin user?
|
|
|
|
// should we handle this case?
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $defaultOwner->getDbId();
|
|
|
|
}
|
|
|
|
} catch(Exception $e) {
|
|
|
|
Logging::info($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* It's possible that the year tag will be a timestamp but Airtime doesn't support this.
|
|
|
|
* The year field in cc_files can only be 16 chars max.
|
|
|
|
*
|
|
|
|
* This functions strips the year field of it's timestamp, if one, and leaves just the year
|
|
|
|
*/
|
|
|
|
private static function stripTimeStampFromYearTag($metadata)
|
|
|
|
{
|
|
|
|
if (isset($metadata["year"])) {
|
|
|
|
if (preg_match("/^(\d{4})-(\d{2})-(\d{2})(?:\s+(\d{2}):(\d{2}):(\d{2}))?$/", $metadata["year"])) {
|
|
|
|
$metadata["year"] = substr($metadata["year"], 0, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function stripInvalidUtf8Characters($string)
|
|
|
|
{
|
|
|
|
//Remove invalid UTF-8 characters
|
|
|
|
//reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?
|
|
|
|
$string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
|
|
|
|
'|[\x00-\x7F][\x80-\xBF]+'.
|
|
|
|
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
|
|
|
|
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
|
|
|
|
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
|
|
|
|
'?', $string );
|
|
|
|
|
|
|
|
//reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
|
|
|
|
$string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
|
|
|
|
'|\xED[\xA0-\xBF][\x80-\xBF]/S','?', $string );
|
|
|
|
|
|
|
|
//Do a final encoding conversion to
|
|
|
|
$string = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function removeEmptySubFolders($path)
|
|
|
|
{
|
|
|
|
exec("find $path -empty -type d -delete");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-30 17:12:47 +01:00
|
|
|
/**
|
|
|
|
* Checks if the file is a regular file that can be previewed and downloaded.
|
|
|
|
*/
|
2014-11-12 20:42:34 +01:00
|
|
|
public function isValidPhysicalFile()
|
2014-07-24 22:56:15 +02:00
|
|
|
{
|
|
|
|
return is_file($this->getAbsoluteFilePath());
|
|
|
|
}
|
|
|
|
|
2014-08-12 18:32:49 +02:00
|
|
|
/**
|
|
|
|
*
|
2014-10-30 17:12:47 +01:00
|
|
|
* Deletes the file from the stor directory on disk.
|
2014-08-12 18:32:49 +02:00
|
|
|
*/
|
2014-07-29 21:07:51 +02:00
|
|
|
public function deletePhysicalFile()
|
|
|
|
{
|
2014-08-01 05:11:49 +02:00
|
|
|
$filepath = $this->getAbsoluteFilePath();
|
|
|
|
if (file_exists($filepath)) {
|
|
|
|
unlink($filepath);
|
|
|
|
} else {
|
|
|
|
throw new Exception("Could not locate file ".$filepath);
|
|
|
|
}
|
2014-07-29 21:07:51 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:42:34 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This function refers to the file's Amazon S3 resource id.
|
|
|
|
* Returns null because cc_files are stored on local disk.
|
|
|
|
*/
|
|
|
|
public function getResourceId()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:49:57 +01:00
|
|
|
public function getCcFileId()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2011-01-10 19:24:21 +01:00
|
|
|
} // CcFiles
|