$config = CRM_Core_Config::singleton();
- $fName = $value['name'];
- $mimeType = $value['type'];
-
// If we are already passing the file id as a value then retrieve and set the file data
if (CRM_Utils_Rule::integer($value)) {
$fileDAO = new CRM_Core_DAO_File();
$mimeType = $fileDAO->mime_type;
}
}
+ else {
+ $fName = $value['name'];
+ $mimeType = $value['type'];
+ }
$filename = pathinfo($fName, PATHINFO_BASENAME);
CRM_Core_Error::fatal('Request signature is invalid');
}
- CRM_Core_BAO_File::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']);
+ self::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']);
}
if (in_array($field, $htmlType)) {
$fileValues = CRM_Core_BAO_File::path($value, $oldEventID);
$customParams["custom_{$field}_-1"] = array(
- 'name' => $fileValues[0],
+ 'name' => CRM_Utils_File::duplicate($fileValues[0]),
'type' => $fileValues[1],
);
}
* The directory where the file should be saved.
* @param string $contents
* Optional: the contents of the file.
+ * @param string $fileName
*
* @return string
* The filename saved, or FALSE on failure.
*/
- public static function createFakeFile($dir, $contents = 'delete me') {
+ public static function createFakeFile($dir, $contents = 'delete me', $fileName = NULL) {
$dir = self::addTrailingSlash($dir);
- $file = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC);
- $success = file_put_contents($dir . $file, $contents);
+ if (!$fileName) {
+ $fileName = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC);
+ }
+ $success = file_put_contents($dir . $fileName, $contents);
- return ($success === FALSE) ? FALSE : $file;
+ return ($success === FALSE) ? FALSE : $fileName;
}
/**
}
}
+ /**
+ * Copies a file
+ *
+ * @param $filePath
+ * @return mixed
+ */
+ public static function duplicate($filePath) {
+ $oldName = pathinfo($filePath, PATHINFO_FILENAME);
+ $uniqID = md5(uniqid(rand(), TRUE));
+ $newName = preg_replace('/(_[\w]{32})$/', '', $oldName) . '_' . $uniqID;
+ $newPath = str_replace($oldName, $newName, $filePath);
+ copy($filePath, $newPath);
+ return $newPath;
+ }
+
/**
* Get files for the extension.
*
}
}
+ public function fileExtensions() {
+ return array(
+ array('txt'),
+ array('danger'),
+ );
+ }
+
+ /**
+ * @dataProvider fileExtensions
+ * @param string $ext
+ */
+ public function testDuplicate($ext) {
+ $fileName = CRM_Utils_File::makeFileName('test' . rand(100, 999) . ".$ext");
+ CRM_Utils_File::createFakeFile('/tmp', 'test file content', $fileName);
+ $newFile = CRM_Utils_File::duplicate("/tmp/$fileName");
+ $this->assertNotEquals("/tmp/$fileName", $newFile);
+ $contents = file_get_contents($newFile);
+ $this->assertEquals('test file content', $contents);
+ unlink("/tmp/$fileName");
+ unlink($newFile);
+ }
+
}