Commit 0bf1ef67 authored by Carsten  Rose's avatar Carsten Rose
Browse files

F4901: implement 'delete split files' if primary file is deleted (record) or...

F4901: implement 'delete split files' if primary file is deleted (record) or exchanged against a new one.
parent 477f6997
Pipeline #1041 passed with stage
in 1 minute and 53 seconds
...@@ -57,7 +57,6 @@ class Delete { ...@@ -57,7 +57,6 @@ class Delete {
* @throws UserFormException * @throws UserFormException
*/ */
public function process($tableName, $recordId, $primaryKey = F_PRIMARY_KEY_DEFAULT) { public function process($tableName, $recordId, $primaryKey = F_PRIMARY_KEY_DEFAULT) {
$msg = array();
if ($tableName === false || $tableName === '') { if ($tableName === false || $tableName === '') {
throw new CodeException('Missing table name', ERROR_MISSING_TABLE_NAME); throw new CodeException('Missing table name', ERROR_MISSING_TABLE_NAME);
...@@ -96,7 +95,7 @@ class Delete { ...@@ -96,7 +95,7 @@ class Delete {
/** /**
* Iterates over array $row and searches for column names with substring COLUMN_PATH_FILE_NAME. * Iterates over array $row and searches for column names with substring COLUMN_PATH_FILE_NAME.
* For any found, check if it references a writeable file. * For any found, check if it references a writable file.
* If yes: check if there are other records (same table, same column) which references the same file. * If yes: check if there are other records (same table, same column) which references the same file.
* If no: delete the file * If no: delete the file
* If yes: do nothing, continue with the next column. * If yes: do nothing, continue with the next column.
...@@ -125,8 +124,10 @@ class Delete { ...@@ -125,8 +124,10 @@ class Delete {
$samePathFileName = $this->db->sql("SELECT COUNT($primaryKey) AS cnt FROM $tableName WHERE $key LIKE ?", ROW_EXPECT_1, [$file]); $samePathFileName = $this->db->sql("SELECT COUNT($primaryKey) AS cnt FROM $tableName WHERE $key LIKE ?", ROW_EXPECT_1, [$file]);
if ($samePathFileName['cnt'] === 1) { if ($samePathFileName['cnt'] === 1) {
HelperFile::unlink($file); HelperFile::unlink($file);
$this->db->deleteSplitFileAndRecord($row[$primaryKey], $tableName);
} }
} }
} }
} }
} }
\ No newline at end of file
...@@ -588,6 +588,7 @@ class Save { ...@@ -588,6 +588,7 @@ class Save {
$arr = $sip->getVarsFromSip($sipUpload); $arr = $sip->getVarsFromSip($sipUpload);
$oldFile = $arr[EXISTING_PATH_FILE_NAME]; $oldFile = $arr[EXISTING_PATH_FILE_NAME];
if (file_exists($oldFile)) { if (file_exists($oldFile)) {
//TODO: it might be possible to delete a file, which is referenced by another record - a check would be nice.
HelperFile::unlink($oldFile); HelperFile::unlink($oldFile);
} }
$flagDelete = ($oldFile != ''); $flagDelete = ($oldFile != '');
...@@ -743,7 +744,6 @@ class Save { ...@@ -743,7 +744,6 @@ class Save {
* *
* @param array $formElement * @param array $formElement
* @param array $statusUpload * @param array $statusUpload
*
* @return array|mixed|null|string * @return array|mixed|null|string
* @throws CodeException * @throws CodeException
* @throws DbException * @throws DbException
...@@ -920,6 +920,11 @@ class Save { ...@@ -920,6 +920,11 @@ class Save {
ERROR_PDF2JPEG); ERROR_PDF2JPEG);
} }
$xId = $this->store->getVar(COLUMN_ID, STORE_RECORD);
// Clean optional existing old split records and files from further uploads.
$this->db->deleteSplitFileAndRecord($xId, $fileSplitTableName);
// IM 'convert' will produce files <file>-1.jpg <file>-10.jpg ... - bring them in natural sort order // IM 'convert' will produce files <file>-1.jpg <file>-10.jpg ... - bring them in natural sort order
natsort($files); natsort($files);
...@@ -927,8 +932,6 @@ class Save { ...@@ -927,8 +932,6 @@ class Save {
$tableName = TABLE_NAME_SPLIT; $tableName = TABLE_NAME_SPLIT;
$sql = "INSERT INTO $tableName (`tableName`, `xId`, `pathFilename`) VALUES (?,?,?)"; $sql = "INSERT INTO $tableName (`tableName`, `xId`, `pathFilename`) VALUES (?,?,?)";
$xId = $this->store->getVar(COLUMN_ID, STORE_RECORD);
// 1) Move split files to final location. 2) Created records to reference each split file. // 1) Move split files to final location. 2) Created records to reference each split file.
foreach ($files as $file) { foreach ($files as $file) {
......
...@@ -985,4 +985,28 @@ class Database { ...@@ -985,4 +985,28 @@ class Database {
public function getQfqLogFile() { public function getQfqLogFile() {
return ($this->store == null) ? SYSTEM_QFQ_LOG_FILE : $this->store->getVar(SYSTEM_QFQ_LOG, STORE_SYSTEM); return ($this->store == null) ? SYSTEM_QFQ_LOG_FILE : $this->store->getVar(SYSTEM_QFQ_LOG, STORE_SYSTEM);
} }
/**
* Selects in table 'Split' all records with tableName='$tableName' and xId='$xId'. Deletes all referenced files and records.
*
* @param $xId
* @param $tableName
* @throws CodeException
* @throws DbException
* @throws UserFormException
*/
public function deleteSplitFileAndRecord($xId, $tableName) {
$sql = 'SELECT pathFileName FROM ' . TABLE_NAME_SPLIT . ' WHERE tableName=? AND xId=?';
$data = $this->sql($sql, ROW_REGULAR, [$tableName, $xId]);
foreach ($data AS $row) {
if (!empty($row['pathFileName']) && is_writable($row['pathFileName'])) {
HelperFile::unlink($row['pathFileName']);
}
}
$this->sql('DELETE FROM ' . TABLE_NAME_SPLIT . ' WHERE tableName=? AND xId=?', ROW_REGULAR, [$tableName, $xId]);
}
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment