Skip to content

Commit

Permalink
Improve logging and better handle a failure case
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Dec 4, 2023
1 parent 4d5bf5d commit b1a2403
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Resumable.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ public function preProcess(): void
if (!empty($this->request->getUploadedFiles())) {
$this->extension = $this->findExtension($this->resumableParam('filename'));
$this->originalFilename = $this->resumableParam('filename');
$this->log('Defined extension: ' . $this->extension . ' for: ' . $this->originalFilename);
$this->log(
'Defined extension',
[
'extension' => $this->extension,
'originalFilename' => $this->originalFilename,
]
);
}
}
}
Expand Down Expand Up @@ -279,12 +285,12 @@ public function handleChunk()
if ($firstFile instanceof UploadedFileInterface) {
$chunkDir = $this->tmpChunkDir($identifier) . DIRECTORY_SEPARATOR;
$chunkFile = $chunkDir . $this->tmpChunkFilename($filename, $chunkNumber);
$this->log('Moving chunk ' . $chunkNumber . ' for ' . $identifier);
$this->log('Moving chunk', ['identifier' => $identifier, 'chunkNumber' => $chunkNumber]);
// On the server that received the upload
$localTempFile = $firstFile->getStream()->getMetadata('uri');
$ressource = fopen($localTempFile, 'r');
if ($ressource === false) {
$this->log('Unable to open the stream for: ' . $localTempFile);
$this->log('Unable to open the stream', ['localTempFile' => $localTempFile]);
return $this->response->withStatus(500);
}

Expand All @@ -303,7 +309,7 @@ public function handleChunk()

if ($this->isFileUploadComplete($filename, $identifier, $chunkSize, $totalSize)) {
$this->isUploadComplete = true;
$this->log('Upload of ' . $identifier . ' is complete');
$this->log('Upload is complete', ['identifier' => $identifier]);
$this->createFileAndDeleteTmp($identifier, $filename);
}

Expand Down Expand Up @@ -332,7 +338,7 @@ private function createFileAndDeleteTmp(string $identifier, ?string $filename):
$this->extension = $this->findExtension($this->filepath);

if ($this->createFileFromChunks($chunkFiles, $this->filepath)) {
$this->log('Upload done for: ' . $identifier);
$this->log('Upload done', ['identifier' => $identifier]);
$this->isUploadComplete = true;
}

Expand All @@ -342,11 +348,11 @@ private function createFileAndDeleteTmp(string $identifier, ?string $filename):
}

foreach ($chunkFiles as $chunkFile) {
$this->log('Removing chunk file: ' . $chunkFile);
$this->log('Removing chunk file', ['chunkFile' => $chunkFile]);
$this->fileSystem->delete($chunkFile);
}

$this->log('Removing chunk dir: ' . $chunkDir);
$this->log('Removing chunk dir', ['chunkDir' => $chunkDir]);

// See: https://github.com/KnpLabs/Gaufrette/issues/524
if (method_exists($this->fileSystem, 'getAdapter')) {
Expand Down Expand Up @@ -419,6 +425,11 @@ public function tmpChunkFilename(string $filename, $chunkNumber): string

public function createFileFromChunks(array $chunkFiles, string $destFile): bool
{
if ($this->fileSystem->has($destFile)) {
$this->log('The final file already exists', ['finalFile' => $destFile]);
return false;
}

$this->log('Beginning of create files from chunks');

natsort($chunkFiles);
Expand All @@ -428,7 +439,7 @@ public function createFileFromChunks(array $chunkFiles, string $destFile): bool

foreach ($chunkFiles as $chunkFile) {
$stream->write($this->fileSystem->read($chunkFile));
$this->log('Append ', ['chunk file' => $chunkFile]);
$this->log('Appending to file', ['chunkFile' => $chunkFile]);
}

$stream->flush();
Expand Down

0 comments on commit b1a2403

Please sign in to comment.