Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add chunked uploads to the postman api description #70

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

gael-connan-cybex
Copy link
Contributor

These examples allow uploading chunks from Postman to the Transmorpher server.

The following code proved useful in debugging this. Some vendor object properties may have to be set to public for this to work.
The main learning was that the filenames of chunks need to be identical when using a tool like Postman.

UploadSlotController
` public function receiveFile(UploadRequest $request, UploadSlot $uploadSlot): JsonResponse|UploadedFile
{
# DEBUG
$uploadSlotIdentifier = $uploadSlot->token;
$requestArray = $request->toArray();
$chunkNumber = $requestArray['dzchunkindex'] ?? $requestArray['chunkNumber'] ?? 'wtf';
Storage::disk()->putFileAs('', $request->file('file'), sprintf('uploadedChunk_%s_%s', $uploadSlotIdentifier, $chunkNumber));
######

    $receiver = new FileReceiver($request->file('file'), $request, HandlerFactory::classFromRequest($request));

    // Check if the chunk is successfully uploaded.
    if ($receiver->isUploaded() === false) {
        throw new UploadMissingFileException();
    }

    $save = $receiver->receive();

    // Check if the full upload has finished and if so, save the file.
    $isFinished = $save->isFinished();

    # DEBUG
    $date                                          = date('YmdHisu');
    $debugArray['requestFields']                   = $requestArray;
    $debugArray['classes']['save']                 = $save::class;
    $debugArray['classes']['handler']              = $save->handler()::class;
    $debugArray['state']['isFinished']             = $isFinished;
    $debugArray['state']['isValid']                = $save->isValid();
    $debugArray['state']['file->isValid']          = $save->file->isValid();
    $debugArray['state']['file->error']            = $save->file->error;
    $debugArray['state']['handler()->isLastChunk()'] = $save->handler()->isLastChunk();
    $debugArray['state']['handler()->foundChunks'] = $save->foundChunks ?? '(doesnt exist)';
    Storage::disk()->put(sprintf('uploadedChunk_%s_%s_data', $uploadSlotIdentifier, $chunkNumber), json_encode($debugArray, JSON_PRETTY_PRINT));
    ######

    if ($isFinished) {
        return $this->saveFile($save->getFile(), $uploadSlot, $uploadSlot->media_type);
    }

    // Full file is not yet uploaded, send the current progress.
    return response()->json([
        "done" => $save->handler()->getPercentageDone(),
    ]);
}`

` protected function saveFile(UploadedFile $uploadedFile, UploadSlot $uploadSlot, MediaType $type): JsonResponse
{
# DEBUG
Storage::disk()->putFileAs('', $uploadedFile, 'uploadedFile.mp4');
#####

...

`

Copy link
Contributor

@mszulik mszulik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally with a file chunked by the Transmorpher Client backend.

Nice and easy addition to the postman file!

Copy link
Contributor Author

@gael-connan-cybex gael-connan-cybex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested with newest postman v11.28.0. works without auth for upload.

@mszulik mszulik mentioned this pull request Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants