Merge branch 'cc-5709-airtime-analyzer-store-file-size-and-hash-in-db' into saas-store-file-size-and-hash-in-db
Conflicts: airtime_mvc/application/Bootstrap.php airtime_mvc/application/modules/rest/controllers/MediaController.php
This commit is contained in:
commit
c1086842d3
|
@ -18,6 +18,7 @@ require_once 'Preference.php';
|
||||||
require_once 'Locale.php';
|
require_once 'Locale.php';
|
||||||
require_once "DateHelper.php";
|
require_once "DateHelper.php";
|
||||||
require_once "LocaleHelper.php";
|
require_once "LocaleHelper.php";
|
||||||
|
require_once "FileDataHelper.php";
|
||||||
require_once "HTTPHelper.php";
|
require_once "HTTPHelper.php";
|
||||||
require_once "OsPath.php";
|
require_once "OsPath.php";
|
||||||
require_once "Database.php";
|
require_once "Database.php";
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: sourcefabric
|
||||||
|
* Date: 17/02/15
|
||||||
|
*/
|
||||||
|
|
||||||
|
class FileDataHelper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We want to throw out invalid data and process the upload successfully
|
||||||
|
* at all costs, so check the data and sanitize it if necessary
|
||||||
|
* @param array $data array containing new file metadata
|
||||||
|
*/
|
||||||
|
public static function sanitizeData(&$data) {
|
||||||
|
// If the track number isn't numeric, this will return 0
|
||||||
|
$data["track_number"] = intval($data["track_number"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -77,8 +77,6 @@ class LibraryController extends Zend_Controller_Action
|
||||||
|
|
||||||
$obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME);
|
$obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME);
|
||||||
if (isset($obj_sess->id)) {
|
if (isset($obj_sess->id)) {
|
||||||
$objInfo = Application_Model_Library::getObjInfo($obj_sess->type);
|
|
||||||
|
|
||||||
$objInfo = Application_Model_Library::getObjInfo($obj_sess->type);
|
$objInfo = Application_Model_Library::getObjInfo($obj_sess->type);
|
||||||
$obj = new $objInfo['className']($obj_sess->id);
|
$obj = new $objInfo['className']($obj_sess->id);
|
||||||
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
|
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
|
||||||
|
@ -447,6 +445,8 @@ class LibraryController extends Zend_Controller_Action
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($form->isValid($serialized)) {
|
if ($form->isValid($serialized)) {
|
||||||
|
// Sanitize any incorrect metadata that slipped past validation
|
||||||
|
FileDataHelper::sanitizeData($serialized["track_number"]);
|
||||||
|
|
||||||
$formValues = $this->_getParam('data', null);
|
$formValues = $this->_getParam('data', null);
|
||||||
$formdata = array();
|
$formdata = array();
|
||||||
|
|
|
@ -59,7 +59,7 @@ class Application_Form_EditAudioMD extends Zend_Form
|
||||||
$track_number->class = 'input_text';
|
$track_number->class = 'input_text';
|
||||||
$track_number->setLabel('Track Number:')
|
$track_number->setLabel('Track Number:')
|
||||||
->setFilters(array('StringTrim'))
|
->setFilters(array('StringTrim'))
|
||||||
->setValidators(array(new Zend_Validate_Digits()));
|
->setValidators(array(new Zend_Validate_Int()));
|
||||||
$this->addElement($track_number);
|
$this->addElement($track_number);
|
||||||
|
|
||||||
// Add genre field
|
// Add genre field
|
||||||
|
|
|
@ -119,6 +119,9 @@ class Rest_MediaController extends Zend_Rest_Controller
|
||||||
$file->save();
|
$file->save();
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
// Sanitize any incorrect metadata that slipped past validation
|
||||||
|
FileDataHelper::sanitizeData($whiteList["track_number"]);
|
||||||
|
|
||||||
/* If full_path is set, the post request came from ftp.
|
/* If full_path is set, the post request came from ftp.
|
||||||
* Users are allowed to upload folders via ftp. If this is the case
|
* 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
|
* we need to include the folder name with the file name, otherwise
|
||||||
|
@ -172,6 +175,9 @@ class Rest_MediaController extends Zend_Rest_Controller
|
||||||
$file->save();
|
$file->save();
|
||||||
return;
|
return;
|
||||||
} else if ($file && isset($requestData["resource_id"])) {
|
} else if ($file && isset($requestData["resource_id"])) {
|
||||||
|
// Sanitize any incorrect metadata that slipped past validation
|
||||||
|
FileDataHelper::sanitizeData($whiteList["track_number"]);
|
||||||
|
|
||||||
$file->fromArray($whiteList, BasePeer::TYPE_FIELDNAME);
|
$file->fromArray($whiteList, BasePeer::TYPE_FIELDNAME);
|
||||||
|
|
||||||
//store the original filename
|
//store the original filename
|
||||||
|
@ -200,8 +206,12 @@ class Rest_MediaController extends Zend_Rest_Controller
|
||||||
->setHttpResponseCode(200)
|
->setHttpResponseCode(200)
|
||||||
->appendBody(json_encode(CcFiles::sanitizeResponse($file)));
|
->appendBody(json_encode(CcFiles::sanitizeResponse($file)));
|
||||||
} else if ($file) {
|
} else if ($file) {
|
||||||
|
// Sanitize any incorrect metadata that slipped past validation
|
||||||
|
$this->sanitizeData($file, $whiteList);
|
||||||
|
|
||||||
//local file storage
|
//local file storage
|
||||||
$file->setDbDirectory(self::MUSIC_DIRS_STOR_PK);
|
$file->setDbDirectory(self::MUSIC_DIRS_STOR_PK);
|
||||||
|
|
||||||
$file->fromArray($whiteList, BasePeer::TYPE_FIELDNAME);
|
$file->fromArray($whiteList, BasePeer::TYPE_FIELDNAME);
|
||||||
//Our RESTful API takes "full_path" as a field, which we then split and translate to match
|
//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
|
//our internal schema. Internally, file path is stored relative to a directory, with the directory
|
||||||
|
@ -299,7 +309,7 @@ class Rest_MediaController extends Zend_Rest_Controller
|
||||||
$fileForm = new Application_Form_EditAudioMD();
|
$fileForm = new Application_Form_EditAudioMD();
|
||||||
$fileForm->startForm($file->getDbId());
|
$fileForm->startForm($file->getDbId());
|
||||||
$fileForm->populate($whiteList);
|
$fileForm->populate($whiteList);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Here we are truncating metadata of any characters greater than the
|
* 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
|
* max string length set in the database. In the rare case a track's
|
||||||
|
|
|
@ -4,6 +4,8 @@ import mutagen
|
||||||
import magic
|
import magic
|
||||||
import wave
|
import wave
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import hashlib
|
||||||
from analyzer import Analyzer
|
from analyzer import Analyzer
|
||||||
|
|
||||||
class MetadataAnalyzer(Analyzer):
|
class MetadataAnalyzer(Analyzer):
|
||||||
|
@ -96,6 +98,23 @@ class MetadataAnalyzer(Analyzer):
|
||||||
#If we couldn't figure out the track_number or track_total, just ignore it...
|
#If we couldn't figure out the track_number or track_total, just ignore it...
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Get file size and md5 hash of the file
|
||||||
|
try:
|
||||||
|
metadata["filesize"] = os.path.getsize(filename)
|
||||||
|
|
||||||
|
with open(filename, 'rb') as fh:
|
||||||
|
m = hashlib.md5()
|
||||||
|
while True:
|
||||||
|
data = fh.read(8192)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
m.update(data)
|
||||||
|
metadata["md5_hash"] = m.hexdigest()
|
||||||
|
except (OSError, IOError) as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#We normalize the mutagen tags slightly here, so in case mutagen changes,
|
#We normalize the mutagen tags slightly here, so in case mutagen changes,
|
||||||
#we find the
|
#we find the
|
||||||
mutagen_to_airtime_mapping = {
|
mutagen_to_airtime_mapping = {
|
||||||
|
|
Loading…
Reference in New Issue