Skip to content

Commit

Permalink
Fix bug in Commons uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Oct 31, 2022
1 parent 190ce18 commit eb219f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Controller/CommonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ public function save(
$formattedVal = is_string($val) ? $val : var_export($val, true);
$this->addFlash(self::FLASH_ERROR, "Unable to upload to Commons. $key: $formattedVal");
}
} else {
$this->addFlash(self::FLASH_SUCCESS, 'Uploaded: ' . $uploaded['url']);
}

$this->addFlash(self::FLASH_SUCCESS, 'Uploaded: ' . $uploaded['url']);
return $this->redirectToRoute('post_view', ['id' => $id]);
}
}
11 changes: 11 additions & 0 deletions src/Filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public function getLocalTempFilepath(File $file): string
return $this->tempRoot() . $tempFilePath;
}

/**
* Remove the local temp copy of a file.
*
* @return bool Whether the deletion succeeded.
*/
public function removeLocalTempFile(File $file): bool
{
$tempFilePath = 'local_tmp/' . $file->getPost()->getId() . '.' . $file->getExtension();
return $this->temp()->delete($tempFilePath);
}

public function write(Filesystem $fs, File $file, string $filePath)
{
$storagePath = $this->getDataStoragePath($file);
Expand Down
14 changes: 3 additions & 11 deletions src/Repository/CommonsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,12 @@ public function getCommonsUrl(): string
*/
public function upload(Post $post, string $title, string $text, string $caption, array $depicts): array
{
//$outStream = $this->filesystems->read($post->getFile(), File::SIZE_FULL);
$tempFilePath = 'commons/' . $post->getId() . '.' . $post->getFile()->getExtension();
$tempFs = $this->filesystems->temp();
// if (!$tempFs->has($tempFilePath)) {
// $tempFs->writeStream($tempFilePath, $outStream);
// }
// $fullTempPath = $this->filesystems->tempRoot() . $tempFilePath;
$fullTempPath = $this->filesystems->getLocalTempFilepath($post->getFile(), $tempFilePath);
$fullTempPath = $this->filesystems->getLocalTempFilepath($post->getFile());
$api = $this->getMediaWikiApi();
$uploader = new CommonsFileUploader($api);
$uploadResult = $uploader->uploadWithResult($title, $fullTempPath, $text);
$tempFs->delete($tempFilePath);

if ($uploadResult['upload']['result'] !== 'Success') {
$this->filesystems->removeLocalTempFile($post->getFile());
if (isset($uploadResult['upload']['warnings'])) {
return $uploadResult['upload'];
}

Expand Down

0 comments on commit eb219f2

Please sign in to comment.