diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index d5d4faf80..36542daf6 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -734,7 +734,14 @@ class StoredFile { $storedFile->mtime = $row['mtime']; $storedFile->md5 = $row['md5']; $storedFile->filepath = $row['filepath']; - $storedFile->exists = TRUE; + + if(file_exists($row['filepath'])) { + $storedFile->exists = true; + } + else { + $storedFile->exists = false; + } + $storedFile->setFormat($row['ftype']); return $storedFile; } @@ -867,7 +874,7 @@ class StoredFile { * local path * @return TRUE|PEAR_Error */ - public function replaceFile($p_localFilePath) + public function replaceFile($p_localFilePath, $p_copyMedia=TRUE) { // Dont do anything if the source and destination files are // the same. @@ -881,7 +888,7 @@ class StoredFile { return $r; } } - return $this->addFile($p_localFilePath); + return $this->addFile($p_localFilePath, $p_copyMedia); } @@ -1783,8 +1790,17 @@ class StoredFile { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": ' . $duplicate->getMessage() .'}}'); } else { - $duplicateName = $duplicate->getMetadataValue(UI_MDATA_KEY_TITLE); - die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "An identical audioclip named ' . $duplicateName . ' already exists in the storage server."}}'); + if(file_exists($duplicate->getRealFilePath())) { + $duplicateName = $duplicate->getMetadataValue(UI_MDATA_KEY_TITLE); + die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "An identical audioclip named ' . $duplicateName . ' already exists in the storage server."}}'); + } + else{ + $res = $duplicate->replaceFile($audio_file); + if (PEAR::isError($res)) { + die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": ' . $duplicate->getMessage() .'}}'); + } + return $duplicate; + } } } diff --git a/utils/airtime-import.php b/utils/airtime-import.php index 616ef3fbd..4bec7614d 100644 --- a/utils/airtime-import.php +++ b/utils/airtime-import.php @@ -112,6 +112,7 @@ function import_audio_file($p_filepath, $p_importMode = null, $p_testOnly = fals global $STORAGE_SERVER_PATH; global $g_fileCount; global $g_duplicates; + global $g_replaced; // Check parameters $p_importMode = strtolower($p_importMode); @@ -204,9 +205,32 @@ function import_audio_file($p_filepath, $p_importMode = null, $p_testOnly = fals // Look up md5sum in database $duplicate = StoredFile::RecallByMd5($md5sum); - if ($duplicate) { - echo "DUPLICATE: $p_filepath\n"; - $g_duplicates++; + if (PEAR::isError($duplicate)) { + echo $duplicate->getMessage(); + echo "\n"; + } + + //row exists in database + if(isset($duplicate)) { + if (file_exists($duplicate->getRealFilePath())) { + echo "DUPLICATE: $p_filepath\n"; + $g_duplicates++; + } + else{ + if ($p_importMode == "copy") { + $doCopyFiles = true; + } + else if ($p_importMode == "link") { + $doCopyFiles = false; + } + $res = $duplicate->replaceFile($p_filepath, $doCopyFiles); + if (PEAR::isError($res)) { + echo $res->getMessage(); + echo "\n"; + return; + } + $g_replaced++; + } return; } @@ -323,7 +347,10 @@ if ( ($importMode == "copy") && !is_writable($CC_CONFIG["storageDir"])) { global $g_fileCount; global $g_duplicates; +global $g_replaced; $g_fileCount = 0; +$g_duplicates = 0; +$g_replaced = 0; if (is_array($files)) { foreach ($files as $filepath) { // absolute path @@ -356,6 +383,7 @@ if ($importMode == "copy") { echo " *** Destination folder: ".$CC_CONFIG['storageDir']."\n"; } echo " *** Files imported: $g_fileCount\n"; +echo " *** Files restored: $g_replaced\n"; echo " *** Duplicate files (not imported): $g_duplicates\n"; if ($g_errors > 0) { echo " *** Errors: $g_errors\n";