Skip to content

Commit

Permalink
Override Filename from Content-Disposition Header
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Neuhaus committed Mar 12, 2018
1 parent 716136a commit b24776c
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions Classes/Mapping/FalMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ protected function downloadFileAndGetFileObject($objectId, array $data, Event $e
}
}

$queryBuilder = (new ConnectionPool())->getConnectionForTable('sys_file')->createQueryBuilder();
$query = $queryBuilder->select('*')
->from('sys_file')
->where('remote_id = :objectId')
->setParameter('objectId', $objectId);
$existingFileRows = $query->execute()->fetchAll();

if ($folder->hasFile($targetFilename)) {
/** @var FileInterface $file */
$file = reset($this->getObjectRepository()->searchByName($folder, $targetFilename)) ?: null;
Expand All @@ -162,15 +169,23 @@ protected function downloadFileAndGetFileObject($objectId, array $data, Event $e
}

if ($download) {
//echo 'Downloading: ' . $targetFolder . $targetFilename . PHP_EOL;
// echo 'Downloading: ' . $targetFolder . $targetFilename . PHP_EOL;
try {
$tempPathAndFilename = $client->saveDerivate($tempPathAndFilename, $event->getObjectId(), $event->getModule()->getUsageFlag());
$contents = file_get_contents($tempPathAndFilename);
unlink($tempPathAndFilename);
if (pathinfo($targetFilename, PATHINFO_EXTENSION) !== pathinfo($tempPathAndFilename, PATHINFO_EXTENSION)) {
$targetFilename = $this->sanitizeFileName(pathinfo($tempPathAndFilename, PATHINFO_BASENAME));
}
$targetFilename = $this->sanitizeFileName(pathinfo($tempPathAndFilename, PATHINFO_BASENAME));
$file = $folder->createFile($targetFilename);
foreach($existingFileRows as $existingFileRow) {
if($existingFileRow['name'] !== $targetFilename) {
$existingFile = $storage->getFile($existingFileRow['identifier']);
try {
$storage->deleteFile($existingFile);
} catch(\Exception $e) {

}
}
}
} catch (ExistingTargetFileNameException $error) {
$file = reset($this->getObjectRepository()->searchByName($folder, $targetFilename));
} catch (ApiException $error) {
Expand All @@ -193,7 +208,6 @@ protected function downloadFileAndGetFileObject($objectId, array $data, Event $e
throw new \RuntimeException('Unable to either create or re-use existing file: ' . $targetFolder . $targetFilename, 1508242161);
}

$queryBuilder = (new ConnectionPool())->getConnectionForTable('sys_file')->createQueryBuilder();
$query = $queryBuilder->update('sys_file', 'f')
->set('f.remote_id', $objectId)
->where($queryBuilder->expr()->eq('f.uid', $file->getUid()))
Expand Down Expand Up @@ -271,6 +285,9 @@ public function check(ApiClient $client, Module $module, array $status)
$events = $client->getEvents($module->getConnectorName(), 0);
$ids = [];
foreach($events as $event) {
if ($event['event_type'] === 0) {
continue;
}
$ids[] = $event['object_id'];
if (count($ids) == 3) {
break;
Expand Down Expand Up @@ -320,8 +337,7 @@ public function check(ApiClient $client, Module $module, array $status)
';
}
if (count($files)) {
$temporaryFile = GeneralUtility::tempnam('derivative_');
$client->saveDerivate($temporaryFile, $ids[0]);
$temporaryFile = $client->saveDerivate(GeneralUtility::tempnam('derivative_'), $ids[0]);
if (!file_exists($temporaryFile) || !filesize($temporaryFile)) {
$status['class'] = 'danger';
$messages['derivative_download_failed'] = sprintf('
Expand Down

0 comments on commit b24776c

Please sign in to comment.