Skip to content

Commit

Permalink
Ensures legacy exports will still be importable.
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricbonhomme committed Nov 30, 2017
1 parent 38bf3f9 commit 021e8a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
21 changes: 16 additions & 5 deletions src/MonarcFO/Service/AnrAssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class AnrAssetService extends \MonarcCore\Service\AbstractService
*/
public function importFromFile($anrId, $data)
{
// Ensure we either have a password, or an empty string (and not 'null')
$key = empty($data['password']) ? '' : $data['password'];

// We can have multiple files imported with the same password (we'll emit warnings if the password mismatches)
if (empty($data['file'])) {
throw new \MonarcCore\Exception\Exception('File missing', 412);
Expand All @@ -58,7 +55,21 @@ public function importFromFile($anrId, $data)

foreach ($data['file'] as $f) {
if (isset($f['error']) && $f['error'] === UPLOAD_ERR_OK && file_exists($f['tmp_name'])) {
$file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true);
$file = [];
if (empty($data['password'])) {
$file = json_decode(trim(file_get_contents($f['tmp_name'])), true);
if ($file == false) { // support legacy export which were base64 encoded
$file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), '')), true);
}
} else {
// Decrypt the file and store the JSON data as an array in memory
$key = $data['password'];
$file = json_decode(trim($this->decrypt(file_get_contents($f['tmp_name']), $key)), true);
if ($file == false) { // support legacy export which were base64 encoded
$file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true);
}
}

if ($file !== false && ($id = $this->get('objectExportService')->importFromArray($file, $anr)) !== false) {
$ids[] = $id;
} else {
Expand Down Expand Up @@ -306,4 +317,4 @@ public function importFromArray($data, $anr, &$objectsCache = [])
}
return false;
}
}
}
21 changes: 15 additions & 6 deletions src/MonarcFO/Service/AnrObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class AnrObjectService extends \MonarcCore\Service\ObjectService
*/
public function importFromFile($anrId, $data)
{
// Ensure we either have a password, or an empty string (and not 'null')
$key = empty($data['password']) ? '' : $data['password'];

// Mode may either be 'merge' or 'duplicate'
$mode = empty($data['mode']) ? 'merge' : $data['mode'];

Expand All @@ -48,8 +45,20 @@ public function importFromFile($anrId, $data)
foreach ($data['file'] as $f) {
// Ensure the file has been uploaded properly, silently skip the files that are erroneous
if (isset($f['error']) && $f['error'] === UPLOAD_ERR_OK && file_exists($f['tmp_name'])) {
// Decrypt the file and store the JSON data as an array in memory
$file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true);
$file = [];
if (empty($data['password'])) {
$file = json_decode(trim(file_get_contents($f['tmp_name'])), true);
if ($file == false) { // support legacy export which were base64 encoded
$file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), '')), true);
}
} else {
// Decrypt the file and store the JSON data as an array in memory
$key = $data['password'];
$file = json_decode(trim($this->decrypt(file_get_contents($f['tmp_name']), $key)), true);
if ($file == false) { // support legacy export which were base64 encoded
$file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true);
}
}

if ($file !== false && ($id = $this->get('objectExportService')->importFromArray($file, $anr, $mode)) !== false) {
// Import was successful, store the ID
Expand Down Expand Up @@ -151,4 +160,4 @@ public function importFromCommon($id, $data)
throw new \MonarcCore\Exception\Exception('Object not found', 412);
}
}
}
}

0 comments on commit 021e8a2

Please sign in to comment.