gws.plugin.upload_helper

Manage chunked uploads.

In your action, declare an endpoint with p: ChunkRequest` as a parameter. This endpoint should invoke ``handle_chunk_request:

import gws.plugin.upload_helper as uh

@gws.ext.command.api('myUpload')
def do_upload(self, req, p: uh.ChunkRequest) -> uh.ChunkResponse:
    # check permissions, etc...
    helper = self.root.app.helper('upload')
    return helper.handle_chunk_request(req, p)
    ...

The client sends chunks to this endpoint, one by one. Each chunk contains the file name and total size. The first chunk has an empty uploadUid, indicating a new upload. Subsequent chunks must provide a valid uploadUid. The handler responds with an uploadUid. Each chunk must have a serial number, starting from 0. Chunks can come in any order.

Once the client decides that the upload is complete, it proceeds with invoking some other endpoint of your action, mentioning the uploadUid returned by the first chunk. The endpoint should invoke get_upload to retrieve the final file. The file is stored in a temporary location and should be moved to a permanent location if necessary.

@gws.ext.command.api(‘myProcessUploadedFile’) def do_process(self, req, p: MyProcessRequest):

helper = self.root.app.helper(‘upload’) try:

upload = helper.get_upload(p.uploadUid)

except uh.Error:

…upload not ready yet…

…process(upload.path)

Source code: gws.plugin.upload_helper

Package Contents

class gws.plugin.upload_helper.ChunkRequest(*args, **kwargs)

Bases: gws.Request

Command request.

chunkCount: int
chunkNumber: int
content: bytes
fileName: str
totalSize: int
uploadUid: str = ''
class gws.plugin.upload_helper.ChunkResponse(*args, **kwargs)

Bases: gws.Response

Command response.

uploadUid: str
exception gws.plugin.upload_helper.Error

Bases: gws.Error

Generic GWS error.

class gws.plugin.upload_helper.Object

Bases: gws.Node

GWS object tree node.

get_upload(uid: str) Upload
handle_chunk_request(req: gws.WebRequester, p: ChunkRequest) ChunkResponse
class gws.plugin.upload_helper.Upload(*args, **kwargs)

Bases: gws.Data

Basic data object.

This object can be instantiated by passing one or or dict arguments and/or keyword args. All dicts keys and keywords become attributes of the object.

Accessing an undefined attribute returns None and no error is raised, unless the attribute name starts with an underscore.

fileName: str
path: str
totalSize: int
uploadUid: str