Skip to content

Commit 50eb792

Browse files
committed
#95 - Maintain permissions on all converted/optimized files
1 parent 579c630 commit 50eb792

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/Console/fileconverter.drush.inc

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ function drush_fileconverter_convert($source = NULL, $destination = NULL) {
128128
}
129129
}
130130

131-
// Remember the file stats of the source file.
132-
$file_stat = NULL;
133-
134131
// Normalize the file paths.
135132
$fc = FileConverter\FileConverter::factory();
136133
$stdin = $stdout = FALSE;
@@ -177,9 +174,6 @@ function drush_fileconverter_convert($source = NULL, $destination = NULL) {
177174
return;
178175
}
179176
}
180-
else {
181-
$file_stat = stat($source);
182-
}
183177
}
184178
if ($destination{0} !== '/') {
185179
if ($destination === '-') {
@@ -235,12 +229,6 @@ function drush_fileconverter_convert($source = NULL, $destination = NULL) {
235229
readfile($destination);
236230
unlink($destination);
237231
}
238-
elseif (isset($file_stat)) {
239-
// Preserve the same owner/mode as the source.
240-
chown($destination, $file_stat['uid']);
241-
chgrp($destination, $file_stat['gid']);
242-
chmod($destination, $file_stat['mode']);
243-
}
244232
}
245233

246234
function drush_fileconverter_list($list_name = NULL) {

src/FileConverter.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,26 @@ public function convert($type = 'string', $convert_path = 'null->null', $source
115115
// Select a converter.
116116
$engines = $this->getEngines($convert_path);
117117

118+
// Remember the file stats of the source file.
119+
$file_stat = NULL;
120+
if ($type === 'file' && is_file($source)) {
121+
$file_stat = stat($source);
122+
}
123+
118124
// Attempt to convert the file.
119125
$errors = array();
120126
foreach ($engines as $engine) {
121127
try {
122128
$engine->$convert($source, $destination);
123129
$this->previous_engines[] = $engine;
124130
$return();
131+
132+
// Preserve the same owner/mode as the source.
133+
if ($type === 'file' && is_file($destination) && isset($file_stat)) {
134+
chown($destination, $file_stat['uid']);
135+
chgrp($destination, $file_stat['gid']);
136+
chmod($destination, $file_stat['mode']);
137+
}
125138
return $this;
126139
} catch (\Exception $e) {
127140
$errors[] = $e->getMessage();
@@ -287,13 +300,7 @@ public function optimizeFile($source = NULL, $destination = NULL, $ext = NULL) {
287300
if (!isset($destination)) {
288301
$destination = $source;
289302
}
290-
$engines = $this->getEngines("$ext->$ext");
291-
foreach ($engines as $engine) {
292-
if ($engine->convertFile($source, $destination)) {
293-
return $this;
294-
}
295-
}
296-
return $this;
303+
return $this->convert('file', "$ext->$ext", $source, $destination);
297304
}
298305

299306
public function &setConverter($convert_path = 'null->null', $configuration = 'null:default') {

0 commit comments

Comments
 (0)