summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2018-01-01 19:55:08 +0100
committerFlorian Pritz <bluewind@xinu.at>2018-01-01 19:55:08 +0100
commite8bf42ea4b14764eafa5d67f903fecd33c6c5a38 (patch)
tree98951d9872d0ad82d856df048dd6e43c8c783069
parentb0a7e6f5e762c350110d0eff01968d3c97344b62 (diff)
Only update mtime when uploaded files already exist
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--application/service/files.php23
1 files changed, 17 insertions, 6 deletions
diff --git a/application/service/files.php b/application/service/files.php
index b45ac8e6f..0b0c16978 100644
--- a/application/service/files.php
+++ b/application/service/files.php
@@ -165,6 +165,7 @@ class files {
}
}
+ $new_storage_id_created = false;
if ($storage_id === null) {
$filesize = filesize($new_file);
$mimetype = mimetype($new_file);
@@ -176,19 +177,29 @@ class files {
"date" => time(),
));
$storage_id = $CI->db->insert_id();
+ $new_storage_id_created = true;
}
$data_id = $hash."-".$storage_id;
- // TODO: all this doesn't have to run if the file exists. updating the mtime would be enough
- // that would also be better for COW filesystems
$dir = $CI->mfile->folder($data_id);
file_exists($dir) || mkdir ($dir);
$new_path = $CI->mfile->file($data_id);
- $dest = new \service\storage($new_path);
- $tmpfile = $dest->begin();
- rename($new_file, $tmpfile);
- $dest->commit();
+ // Update mtime for cronjob
+ touch($new_path);
+
+ // touch may create a new file if the cronjob cleaned up in between the db check and here.
+ // In that case the file will be empty so move in the data
+ if ($new_storage_id_created || filesize($new_path) === 0) {
+ $dest = new \service\storage($new_path);
+ $tmpfile = $dest->begin();
+
+ // $new_file may reside on a different file system so this call
+ // could perform a copy operation internally. $dest->commit() will
+ // ensure that it performs an atomic overwrite (rename).
+ rename($new_file, $tmpfile);
+ $dest->commit();
+ }
$CI->mfile->add_file($userid, $id, $filename, $storage_id);
}