Commit a6622ff1 authored by Carsten  Rose's avatar Carsten Rose
Browse files

F4901: implement split with 'convert' (jpeg). Refactor chmod(), unlink(),...

F4901: implement split with 'convert' (jpeg). Refactor chmod(), unlink(), rename(), rmdir(), copy() to use by QFQ version which throws an exception.
parent 4f31433c
Pipeline #1040 passed with stage
in 2 minutes and 13 seconds
...@@ -3570,7 +3570,7 @@ have multiple references to a single file. Therefore this check is just a fallba ...@@ -3570,7 +3570,7 @@ have multiple references to a single file. Therefore this check is just a fallba
.. _Upload simple mode: .. _Upload simple mode:
Upload simple mode Upload simple mode
;;;;;;;;;;;;;;;;;; ''''''''''''''''''
Requires: *'upload'-FormElement.name = 'column name'* of an column in the primary table. Requires: *'upload'-FormElement.name = 'column name'* of an column in the primary table.
...@@ -3588,7 +3588,7 @@ Multiple 'upload'-FormElements per form are possible. Each of it needs an own ta ...@@ -3588,7 +3588,7 @@ Multiple 'upload'-FormElements per form are possible. Each of it needs an own ta
.. _Upload advanced mode: .. _Upload advanced mode:
Upload advanced mode Upload advanced mode
;;;;;;;;;;;;;;;;;;;; ''''''''''''''''''''
Requires: *'upload'-FormElement.name* is unknown as a column in the primary table. Requires: *'upload'-FormElement.name* is unknown as a column in the primary table.
...@@ -3633,23 +3633,26 @@ A typical name for such an 'upload'-FormElement, to show that the name does not ...@@ -3633,23 +3633,26 @@ A typical name for such an 'upload'-FormElement, to show that the name does not
.. _split-pdf-upload: .. _split-pdf-upload:
Split PDF Upload Split PDF Upload
;;;;;;;;;;;;;;;; ''''''''''''''''
Additional to the upload, it's possible to split the uploaded file (only PDF files) into several SVG files, one file per Additional to the upload, it's possible to split the uploaded file (only PDF files) into several SVG or JPEG files, one
page. The split is done via http://www.cityinthesky.co.uk/opensource/pdf2svg/. file per PDF page. The split is done via http://www.cityinthesky.co.uk/opensource/pdf2svg/ or Image Magick `convert`.
* *FormElement.parameter*: * *FormElement.parameter*:
* *fileSplit* = `<type>` - Activate the splitting process. Only possible value: `fileSplit=svg`. * *fileSplit* = `<type>` - Activate the splitting process. Possible values: `svg` or `jpeg`. No default.
* *fileDestinationSplit* = `<pathFileName (pattern)>` - Target directory and filename pattern for the created & split'ed files. E.g. :: * *fileDestinationSplit* = `<pathFileName (pattern)>` - Target directory and filename pattern for the created &
split'ed files. Default <fileDestination>.split/split.<nr>.<fileSplit>
If explicit given, respect that SVG needs a printf style for <nr>, whereas JPEG is numbered automatically. E.g. ::
fileDestinationSplit = fileadmin/protected/{{id:R}}.{{filenameBase}}.%02d.svg [svg] fileDestinationSplit = fileadmin/protected/{{id:R}}.{{filenameBase:V}}.%02d.svg
[jpeg] fileDestinationSplit = fileadmin/protected/{{id:R}}.{{filenameBase:V}}.jpg
* *tableNameSplit* = `<tablename>` - Reference in table 'Split' to the table, which holds the original PDF file. * *tableNameSplit* = `<tablename>` - Default: name of table of current form. This name will be saved in table `Split`
The splitting happens immediately after the user pressed save. The splitting happens immediately after the user pressed `save`.
To easily access the split files via QFQ, per file per record is created in table 'Split'. To easily access the split files via QFQ, per file one record is created in table 'Split'.
Table 'Split': Table 'Split':
...@@ -3664,8 +3667,6 @@ Table 'Split': ...@@ -3664,8 +3667,6 @@ Table 'Split':
+--------------+--------------------------------------------------------------------------------------------+ +--------------+--------------------------------------------------------------------------------------------+
| pathFileName | Path/filename reference to one of the created files | | pathFileName | Path/filename reference to one of the created files |
+--------------+--------------------------------------------------------------------------------------------+ +--------------+--------------------------------------------------------------------------------------------+
| created | Timestamp |
+--------------+--------------------------------------------------------------------------------------------+
One usecase why to split an upload: annotate individual pages by using the `FormElement`.type=`annotate`. One usecase why to split an upload: annotate individual pages by using the `FormElement`.type=`annotate`.
......
...@@ -214,6 +214,7 @@ const ERROR_STORE_KEY_EXIST = 1201; ...@@ -214,6 +214,7 @@ const ERROR_STORE_KEY_EXIST = 1201;
// I/O Error // I/O Error
const ERROR_IO_READ_FILE = 1300; const ERROR_IO_READ_FILE = 1300;
const ERROR_IO_COPY = 1301; const ERROR_IO_COPY = 1301;
const ERROR_IO_RMDIR = 1302;
const ERROR_IO_WRITE = 1303; const ERROR_IO_WRITE = 1303;
const ERROR_IO_OPEN = 1304; const ERROR_IO_OPEN = 1304;
const ERROR_IO_UNLINK = 1305; const ERROR_IO_UNLINK = 1305;
...@@ -227,6 +228,7 @@ const ERROR_IO_COPY_FILE = 1312; ...@@ -227,6 +228,7 @@ const ERROR_IO_COPY_FILE = 1312;
const ERROR_IO_FILE_NOT_FOUND = 1313; const ERROR_IO_FILE_NOT_FOUND = 1313;
const ERROR_IO_CHMOD = 1314; const ERROR_IO_CHMOD = 1314;
const ERROR_PDF2SVG = 1315; const ERROR_PDF2SVG = 1315;
const ERROR_PDF2JPEG = 1316;
//Report //Report
const ERROR_UNKNOWN_LINK_QUALIFIER = 1400; const ERROR_UNKNOWN_LINK_QUALIFIER = 1400;
...@@ -980,6 +982,7 @@ const FE_FILE_MAX_FILE_SIZE = 'maxFileSize'; // Target pathFilename for an uploa ...@@ -980,6 +982,7 @@ const FE_FILE_MAX_FILE_SIZE = 'maxFileSize'; // Target pathFilename for an uploa
const FE_FILE_CAPTURE = 'capture'; // On a smartphone opens the camera const FE_FILE_CAPTURE = 'capture'; // On a smartphone opens the camera
const FE_FILE_SPLIT = 'fileSplit'; const FE_FILE_SPLIT = 'fileSplit';
const FE_FILE_SPLIT_SVG = 'svg'; const FE_FILE_SPLIT_SVG = 'svg';
const FE_FILE_SPLIT_JPEG = 'jpeg';
const FE_FILE_SPLIT_TABLE_NAME = 'tableNameSplit'; const FE_FILE_SPLIT_TABLE_NAME = 'tableNameSplit';
const FE_FILE_DOWNLOAD_BUTTON = 'downloadButton'; const FE_FILE_DOWNLOAD_BUTTON = 'downloadButton';
const FE_FILE_AUTO_ORIENT = 'autoOrient'; const FE_FILE_AUTO_ORIENT = 'autoOrient';
...@@ -1214,9 +1217,13 @@ const UPLOAD_MODE_DELETEOLD = 'deleteOld'; ...@@ -1214,9 +1217,13 @@ const UPLOAD_MODE_DELETEOLD = 'deleteOld';
const UPLOAD_MODE_DELETEOLD_NEW = 'deleteOld+new'; const UPLOAD_MODE_DELETEOLD_NEW = 'deleteOld+new';
const UPLOAD_DEFAULT_MAX_SIZE = '10M'; const UPLOAD_DEFAULT_MAX_SIZE = '10M';
const UPLOAD_DEFAULT_MIME_TYPE = 'application/pdf'; const UPLOAD_DEFAULT_MIME_TYPE = 'application/pdf';
const MIME_TYPE_SPLIT_CAPABLE = 'application/pdf';
// $_FILES // $_FILES
const FILES_NAME = 'name'; const FILES_NAME = 'name';
const FILES_TMP_NAME = 'tmp_name'; const FILES_TMP_NAME = 'tmp_name';
const FILES_TYPE = 'type';
const FILES_ERROR = 'error'; const FILES_ERROR = 'error';
const FILES_SIZE = 'size'; const FILES_SIZE = 'size';
const FILES_FLAG_DELETE = 'flagDelete'; const FILES_FLAG_DELETE = 'flagDelete';
......
...@@ -70,7 +70,7 @@ class Delete { ...@@ -70,7 +70,7 @@ class Delete {
// Take care the necessary target directories exist. // Take care the necessary target directories exist.
$cwd = getcwd(); $cwd = getcwd();
$sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM); $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
if ($cwd === false || $sitePath === false || !chdir($sitePath)) { if ($cwd === false || $sitePath === false || !HelperFile::chdir($sitePath)) {
throw new UserFormException( throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_SUPPORT => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]), json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_SUPPORT => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]),
ERROR_IO_CHDIR); ERROR_IO_CHDIR);
...@@ -91,7 +91,7 @@ class Delete { ...@@ -91,7 +91,7 @@ class Delete {
} }
chdir($cwd); HelperFile::chdir($cwd);
} }
/** /**
...@@ -124,11 +124,7 @@ class Delete { ...@@ -124,11 +124,7 @@ class Delete {
// This check won't find duplicates, if they are spread over different columns or tables. // This check won't find duplicates, if they are spread over different columns or tables.
$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) {
if (!unlink($file)) { HelperFile::unlink($file);
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Error deleting file', ERROR_MESSAGE_SUPPORT => "Error deleting file: $file"]),
ERROR_IO_UNLINK);
}
} }
} }
} }
......
...@@ -155,11 +155,7 @@ class File { ...@@ -155,11 +155,7 @@ class File {
if (isset($statusUpload[FILES_TMP_NAME]) && $statusUpload[FILES_TMP_NAME] != '') { if (isset($statusUpload[FILES_TMP_NAME]) && $statusUpload[FILES_TMP_NAME] != '') {
$file = Support::extendFilename($statusUpload[FILES_TMP_NAME], UPLOAD_CACHED); $file = Support::extendFilename($statusUpload[FILES_TMP_NAME], UPLOAD_CACHED);
if (file_exists($file)) { if (file_exists($file)) {
if (!unlink($file)) { HelperFile::unlink($file);
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Unlink file', ERROR_MESSAGE_SUPPORT => 'unlink file: ' . $file]),
ERROR_IO_UNLINK);
}
} }
$statusUpload[FILES_TMP_NAME] = ''; $statusUpload[FILES_TMP_NAME] = '';
} }
......
...@@ -766,7 +766,7 @@ class QuickFormQuery { ...@@ -766,7 +766,7 @@ class QuickFormQuery {
if (false !== ($arr = stat($file))) { if (false !== ($arr = stat($file))) {
if (time() - $arr['mtime'] > FORM_LOG_FILE_EXPIRE) { if (time() - $arr['mtime'] > FORM_LOG_FILE_EXPIRE) {
unlink($file); HelperFile::unlink($file);
} else { } else {
$form[FORM_LOG_FILE . '_' . $mode] = $file; $form[FORM_LOG_FILE . '_' . $mode] = $file;
$form[FORM_LOG_ACTIVE] = 1; $form[FORM_LOG_ACTIVE] = 1;
......
...@@ -472,7 +472,7 @@ class Save { ...@@ -472,7 +472,7 @@ class Save {
// Take care the necessary target directories exist. // Take care the necessary target directories exist.
$cwd = getcwd(); $cwd = getcwd();
$sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM); $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
if ($cwd === false || $sitePath === false || !chdir($sitePath)) { if ($cwd === false || $sitePath === false || !HelperFile::chdir($sitePath)) {
throw new UserFormException( throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_SUPPORT => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]), json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_SUPPORT => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]),
ERROR_IO_CHDIR); ERROR_IO_CHDIR);
...@@ -520,11 +520,7 @@ class Save { ...@@ -520,11 +520,7 @@ class Save {
if (!file_exists($pathFileName . $extSave) && if (!file_exists($pathFileName . $extSave) &&
!file_exists($pathParts['dirname'] . $pathParts['filename'] . $extSave) !file_exists($pathParts['dirname'] . $pathParts['filename'] . $extSave)
) { ) {
if (!rename($pathFileName, $pathFileName . $extSave)) { HelperFile::rename($pathFileName, $pathFileName . $extSave);
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Rename file', ERROR_MESSAGE_SUPPORT => "Rename file: '$pathFileName' > '$pathFileName$extSave'"]),
ERROR_IO_RENAME);
}
} }
} }
...@@ -581,7 +577,7 @@ class Save { ...@@ -581,7 +577,7 @@ class Save {
// Upload - Take care the necessary target directories exist. // Upload - Take care the necessary target directories exist.
$cwd = getcwd(); $cwd = getcwd();
$sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM); $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
if ($cwd === false || $sitePath === false || !chdir($sitePath)) { if ($cwd === false || $sitePath === false || !HelperFile::chdir($sitePath)) {
throw new UserFormException( throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_SUPPORT => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]), json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_SUPPORT => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]),
ERROR_IO_CHDIR); ERROR_IO_CHDIR);
...@@ -592,11 +588,7 @@ class Save { ...@@ -592,11 +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)) {
if (!unlink($oldFile)) { HelperFile::unlink($oldFile);
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Unlink file failed', ERROR_MESSAGE_SUPPORT => 'Unlink file failed: ' . $oldFile]),
ERROR_IO_UNLINK);
}
} }
$flagDelete = ($oldFile != ''); $flagDelete = ($oldFile != '');
} }
...@@ -613,7 +605,7 @@ class Save { ...@@ -613,7 +605,7 @@ class Save {
$pathFileName = $this->copyUploadFile($formElement, $statusUpload); $pathFileName = $this->copyUploadFile($formElement, $statusUpload);
} }
chdir($cwd); HelperFile::chdir($cwd);
// Delete current used uniq SIP // Delete current used uniq SIP
$this->store->setVar($sipUpload, array(), STORE_EXTRA); $this->store->setVar($sipUpload, array(), STORE_EXTRA);
...@@ -803,9 +795,9 @@ class Save { ...@@ -803,9 +795,9 @@ class Save {
} }
$this->autoOrient($formElement, $pathFileName); $this->autoOrient($formElement, $pathFileName);
HelperFile::doChmod($pathFileName, $chmodFile); HelperFile::chmod($pathFileName, $chmodFile);
$this->splitUpload($formElement, $pathFileName, $chmodFile); $this->splitUpload($formElement, $pathFileName, $chmodFile, $statusUpload);
return $pathFileName; return $pathFileName;
} }
...@@ -856,48 +848,30 @@ class Save { ...@@ -856,48 +848,30 @@ class Save {
* @param array $formElement * @param array $formElement
* @param string $pathFileName * @param string $pathFileName
* @param int $chmod * @param int $chmod
* @param array $statusUpload
* @throws CodeException * @throws CodeException
* @throws DbException * @throws DbException
* @throws UserFormException * @throws UserFormException
* @throws UserReportException * @throws UserReportException
*/ */
private function splitUpload(array $formElement, $pathFileName, $chmod) { private function splitUpload(array $formElement, $pathFileName, $chmod, array $statusUpload) {
if (empty($formElement[FE_FILE_SPLIT]) || empty($formElement[FE_FILE_DESTINATION_SPLIT])) { if (empty($formElement[FE_FILE_SPLIT]) || $statusUpload[FILES_TYPE] != MIME_TYPE_SPLIT_CAPABLE) {
return; return;
} }
$fileDestinationSplit = $this->evaluate->parse($formElement[FE_FILE_DESTINATION_SPLIT]); $fileDestinationSplit = $this->evaluate->parse($formElement[FE_FILE_DESTINATION_SPLIT]);
$fileSplit = $this->evaluate->parse($formElement[FE_FILE_SPLIT]); $fileSplitType = $this->evaluate->parse($formElement[FE_FILE_SPLIT]);
$fileSplitTableName = $this->evaluate->parse($formElement[FE_FILE_SPLIT_TABLE_NAME]); $fileSplitTableName = $this->evaluate->parse($formElement[FE_FILE_SPLIT_TABLE_NAME]);
if (empty($fileSplitTableName)) { if (empty($fileSplitTableName)) {
$fileSplitTableName = $this->formSpec[F_TABLE_NAME]; $fileSplitTableName = $this->formSpec[F_TABLE_NAME];
} }
// Filetype testen: nur Dateien splitten die man auch wirklich entpacken kann if ($fileDestinationSplit == '') {
switch ($fileSplit) { $ext = ($fileSplitType == FE_FILE_SPLIT_SVG) ? '.%02d.svg' : '.jpg';
case FE_FILE_SPLIT_SVG: $fileDestinationSplit = $pathFileName . '.split/split' . $ext;
$this->splitSvg($pathFileName, $fileDestinationSplit, $fileSplitTableName, $chmod);
break;
default:
throw new UserFormException("Unknown 'fileSplit' type: " . $formElement[FE_FILE_SPLIT], ERROR_UNKNOWN_TOKEN);
} }
}
/**
* Split's the PDF file $pathFileNameSrc in several SVG-file, one per page.
* For every created file, a record in table 'Split' is created, storing the pathFileName to the individual file.
*
* @param string $pathFileNameSrc
* @param string $fileDestinationSplit
* @param string $fileSplitTableName
* @param int $chmod
* @throws CodeException
* @throws DbException
* @throws UserFormException
*/
private function splitSvg($pathFileNameSrc, $fileDestinationSplit, $fileSplitTableName, $chmod) {
Support::mkDirParent($fileDestinationSplit); Support::mkDirParent($fileDestinationSplit);
...@@ -907,15 +881,9 @@ class Save { ...@@ -907,15 +881,9 @@ class Save {
// Create temporary directory // Create temporary directory
$tempDir = Support::createTempDir(); $tempDir = Support::createTempDir();
$newSrc = $tempDir . DIRECTORY_SEPARATOR . QFQ_TEMP_SOURCE; $newSrc = $tempDir . DIRECTORY_SEPARATOR . QFQ_TEMP_SOURCE;
if (false === copy($pathFileNameSrc, $newSrc)) { HelperFile::copy($pathFileName, $newSrc);
$msg = HelperFile::getLastErrorAsString() . " - copy $pathFileNameSrc $newSrc";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_COPY);
}
if (false === chdir($tempDir)) { HelperFile::chdir($tempDir);
$msg = HelperFile::getLastErrorAsString() . " - chdir($tempDir)";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'chdir failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_CHDIR);
}
// Split destination. // Split destination.
$pathParts = pathinfo($fileDestinationSplit); $pathParts = pathinfo($fileDestinationSplit);
...@@ -926,21 +894,42 @@ class Save { ...@@ -926,21 +894,42 @@ class Save {
// Extract filename from destination directory. // Extract filename from destination directory.
$fileNameDest = $pathParts['basename']; $fileNameDest = $pathParts['basename'];
switch ($fileSplitType) {
case FE_FILE_SPLIT_SVG:
$cmd = 'pdf2svg "' . $newSrc . '" "' . $fileNameDest . '" all';
break;
case FE_FILE_SPLIT_JPEG:
$cmd = 'convert -quality 90 -density 150 "' . $newSrc . '" "' . $fileNameDest . '"';
break;
default:
throw new UserFormException("Unknown 'fileSplit' type: " . $formElement[FE_FILE_SPLIT], ERROR_UNKNOWN_TOKEN);
}
// Split PDF // Split PDF
$cmd = 'pdf2svg "' . $newSrc . '" "' . $fileNameDest . '" all';
$output = Support::qfqExec($cmd, $rc); $output = Support::qfqExec($cmd, $rc);
if ($rc != 0) { if ($rc != 0) {
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'pdf2svg failed', ERROR_MESSAGE_SUPPORT => "[$cwd][cmd=$cmd]$output"]), throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'pdf2svg failed', ERROR_MESSAGE_SUPPORT => "[$cwd][cmd=$cmd]$output"]),
ERROR_PDF2SVG); ERROR_PDF2SVG);
} }
// Array of created filenames. // Array of created file names.
$files = scandir('.'); if (false === ($files = scandir('.'))) {
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Splitted files not found', ERROR_MESSAGE_SUPPORT => "[cwd=$cwd] scandir(.)" . HelperFile::errorGetLastAsString()]),
ERROR_PDF2JPEG);
}
// IM 'convert' will produce files <file>-1.jpg <file>-10.jpg ... - bring them in natural sort order
natsort($files);
// Create DB records according to the extracted filenames. // Create DB records according to the extracted filenames.
$tableName = TABLE_NAME_SPLIT; $tableName = TABLE_NAME_SPLIT;
$sql = "INSERT INTO $tableName (`tableName`, `xId`, `pathFilename`, `created`) VALUES (?,?,?, NOW())"; $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.
foreach ($files as $file) { foreach ($files as $file) {
if ($file == '.' || $file == '..' || $file == QFQ_TEMP_SOURCE) { if ($file == '.' || $file == '..' || $file == QFQ_TEMP_SOURCE) {
...@@ -953,20 +942,21 @@ class Save { ...@@ -953,20 +942,21 @@ class Save {
$fileDestination = $file; $fileDestination = $file;
} }
$rc = rename($file, Support::joinPath($cwd, $fileDestination)); Support::moveFile($file, Support::joinPath($cwd, $fileDestination), true);
HelperFile::doChmod($fileDestination, $chmod); HelperFile::chmod($fileDestination, $chmod);
// Insert records. // Insert records.
$this->db->sql($sql, ROW_REGULAR, [$fileSplitTableName, $this->store->getVar(COLUMN_ID, STORE_RECORD), $fileDestination]); $this->db->sql($sql, ROW_REGULAR, [$fileSplitTableName, $xId, $fileDestination]);
} }
// Pop directory // Pop directory
$rc = chdir($cwd); HelperFile::chdir($cwd);
// Remove duplicated source // Remove duplicated source
$rc = unlink($newSrc); HelperFile::unlink($newSrc);
// Remove empty directory // Remove empty directory
$rc = rmdir($tempDir); HelperFile::rmdir($tempDir);
} }
/** /**
......
...@@ -590,11 +590,7 @@ class FormAction { ...@@ -590,11 +590,7 @@ class FormAction {
} }
Support::mkDirParent($rowDest[$key]); Support::mkDirParent($rowDest[$key]);
if (!copy($val, $rowDest[$key])) { HelperFile::copy($val, $rowDest[$key]);
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Error copy file', ERROR_MESSAGE_SUPPORT => "Error copy file from [$val] to [" . $rowDest[$key] . "]"]),
ERROR_IO_COPY_FILE);
}
} }
} }
......
...@@ -20,17 +20,18 @@ class HelperFile { ...@@ -20,17 +20,18 @@ class HelperFile {
* Iterate over array $files. Delete only named files which are stored in '/tmp/' . DOWNLOAD_FILE_PREFIX. * Iterate over array $files. Delete only named files which are stored in '/tmp/' . DOWNLOAD_FILE_PREFIX.
* *
* @param array $files * @param array $files
* @throws UserFormException
*/ */
public static function cleanTempFiles(array $files) { public static function cleanTempFiles(array $files) {
foreach ($files as $file) { foreach ($files as $file) {
if (self::isQfqTemp($file)) { if (self::isQfqTemp($file)) {
unlink($file); self::unlink($file);
} }
$dir = dirname($file); $dir = dirname($file);
if (self::isQfqTemp($dir)) { if (self::isQfqTemp($dir)) {
rmdir($dir); self::rmdir($dir);
} }
} }
} }
...@@ -60,10 +61,11 @@ class HelperFile { ...@@ -60,10 +61,11 @@ class HelperFile {
/** /**
* Creates a temporary directory. * Creates a temporary directory.
* @throws UserFormException
*/ */
public static function mktempdir() { public static function mktempdir() {
$name = tempnam(sys_get_temp_dir(), TMP_FILE_PREFIX); $name = tempnam(sys_get_temp_dir(), TMP_FILE_PREFIX);
unlink($name); self::unlink($name);
mkdir($name); mkdir($name);
return $name; return $name;
...@@ -224,25 +226,24 @@ class HelperFile { ...@@ -224,25 +226,24 @@ class HelperFile {
/** /**
* @param $pathFileName * @param $pathFileName
* @param bool $chmod * @param int|bool $mode
* @throws UserFormException * @throws UserFormException
*/ */
public static function doChmod($pathFileName, $chmod = false) { public static function chmod($pathFileName, $mode = false) {
if ($chmod !== false) { if ($mode !== false) {
if (false === chmod($pathFileName, $chmod)) { if (false === chmod($pathFileName, $mode)) {
throw new UserFormException( throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Failed: chmod', ERROR_MESSAGE_SUPPORT => "Failed: chmod $chmod '$pathFileName'"]), json_encode([ERROR_MESSAGE_TO_USER => 'Failed: chmod', ERROR_MESSAGE_SUPPORT => "Failed: chmod $mode '$pathFileName'"]),
ERROR_IO_CHMOD); ERROR_IO_CHMOD);
} }
} }