An implementation of Hyperspace::Stream::Gate for file uploads. More...

#include <HyperspaceStream/FileReceiverGate>

Inheritance diagram for Hyperspace::Stream::FileReceiverGate:

Public Types

enum  OverwritePolicy : quint8 { OverwritePolicy::AppendIncrementalNumber, OverwritePolicy::ReplaceOldFile, OverwritePolicy::RejectNewFile }
 

Signals

void uploadCompleted (const QByteArray &id, const QString &filePath)
 Emitted when a file upload is completed. More...
 
- Signals inherited from Hemera::AsyncInitObject
void ready ()
 

Public Member Functions

 FileReceiverGate (const QList< QByteArray > &capabilities, const QByteArray &baseTarget="/", QObject *parent=nullptr)
 Creates a new Gate and exposes a temporary upload endpoint. More...
 
 FileReceiverGate (const QString &uploadDirectory, const QList< QByteArray > &capabilities, const QByteArray &baseTarget="/", QObject *parent=nullptr)
 Creates a new Gate and exposes an upload endpoint. More...
 
void setOverwritePolicy (OverwritePolicy policy)
 Sets the overwrite policy for this Gate. More...
 
void setFilePermissions (QFileDevice::Permissions permissions)
 Sets the default file permissions for uploaded files. More...
 
- Public Member Functions inherited from Hyperspace::Stream::Gate
virtual ~Gate ()
 default destructor More...
 

Protected Member Functions

virtual void severConnectionImpl (const QByteArray &id) overridefinal
 
virtual void incomingConnection (const QByteArray &id, Hyperspace::Protocol::StreamSocketType type, qintptr fd) overridefinal
 
virtual void initImpl () override
 
- Protected Member Functions inherited from Hyperspace::Stream::Gate
QHash< QByteArray, qintptr > availableConnections () const
 
virtual Rebound validateWave (const Hyperspace::Wave &wave)
 
- Protected Member Functions inherited from Hyperspace::Gate
void assignWaveTarget (AbstractWaveTarget *target)
 Assigns an AbstractWaveTarget to this gate. More...
 
- Protected Member Functions inherited from Hemera::AsyncInitObject
void setParts (uint parts)
 

Additional Inherited Members

- Public Slots inherited from Hyperspace::Stream::Gate
void severAllConnections ()
 Truncates all existing connections. More...
 
void severConnection (const QByteArray &id)
 Truncates a connection to a specific client. More...
 
- Public Slots inherited from Hemera::AsyncInitObject
Hemera::Operationinit ()
 
- Protected Slots inherited from Hemera::AsyncInitObject
void setReady ()
 
void setInitError (const QString &errorName, const QString &message=QString())
 
void setOnePartIsReady ()
 

Detailed Description

An implementation of Hyperspace::Stream::Gate for file uploads.

FilePublisherGate offers a high-performance, non-blocking way for allowing clients to upload files or data to an Application. It can either download files to a temporary or a permanent location.

On the receiving end, the handshake will need to supply two additional attributes, Hyperspace-Stream-File-Size, with the size of the file in bytes, and Hyperspace-Stream-File-Name with the file name. The stream pipe will automatically be truncated on upload completion.

To expose an upload endpoint, simply create a Gate and initialize it.

See also
Hyperspace::Stream::Gate

Member Enumeration Documentation

Defines the overwrite policy in case a file with the same name is uploaded more than once.

Enumerator
AppendIncrementalNumber 

Accepts the upload, and renames the file into $filename.$incrementalnumber.

ReplaceOldFile 

Accepts the upload, truncates the old file and replaces it.

RejectNewFile 

Rejects the upload and keeps the old file.

Constructor & Destructor Documentation

Hyperspace::Stream::FileReceiverGate::FileReceiverGate ( const QList< QByteArray > &  capabilities,
const QByteArray &  baseTarget = "/",
QObject *  parent = nullptr 
)
explicit

Creates a new Gate and exposes a temporary upload endpoint.

Files will be uploaded to a temporary location, and will be deleted when the Gate will be closed. Listen to uploadCompleted to access files.

capabilities The capabilities of the Gate. baseTarget The target of the Gate. parent QObject parent, if any.

Hyperspace::Stream::FileReceiverGate::FileReceiverGate ( const QString &  uploadDirectory,
const QList< QByteArray > &  capabilities,
const QByteArray &  baseTarget = "/",
QObject *  parent = nullptr 
)
explicit

Creates a new Gate and exposes an upload endpoint.

Files will be uploaded to uploadDirectory, which can be either a full filesystem path (in this case, you need to make sure the application can access it) or a "resource://" URL. If uploadDirectory does not exist, it will be created. If the location is unaccessible or cannot be created, initialization will fail.

uploadDirectory The target directory for the uploads. capabilities The capabilities of the Gate. baseTarget The target of the Gate. parent QObject parent, if any.

Member Function Documentation

void Hyperspace::Stream::FileReceiverGate::setOverwritePolicy ( FileReceiverGate::OverwritePolicy  policy)

Sets the overwrite policy for this Gate.

When a file with the same name already exists, the Gate might act depending on its policy. By default, it is AppendIncrementalNumber, but can be altered by calling this method.

policy The overwrite policy to apply to this Gate.

void Hyperspace::Stream::FileReceiverGate::setFilePermissions ( QFileDevice::Permissions  permissions)

Sets the default file permissions for uploaded files.

When a file is uploaded, it is saved to the persistent storage with a set of permissions. By default, those correspond to 0660, but can be altered by calling this method.

permissions The permissions files uploaded through this Gate will have.

void Hyperspace::Stream::FileReceiverGate::severConnectionImpl ( const QByteArray &  id)
finaloverrideprotectedvirtual

Reimplement this method in your gate to sever a connection. Usually, you just need to close() the file descriptor, unless you have been performing some other operations or are using wrappers. Nevertheless, you need to make sure that after calling this method the file descriptor will be closed within this process, and the associated pipe closed.

id The id of the client.

Implements Hyperspace::Stream::Gate.

void Hyperspace::Stream::FileReceiverGate::incomingConnection ( const QByteArray &  id,
Hyperspace::Protocol::StreamSocketType  type,
qintptr  fd 
)
finaloverrideprotectedvirtual

Reimplement this method in your Gate to handle new connections. It will be called every time a stream has been initiated successfully. You can assume fd is already open and ready to stream and receive data without further action.

id The id of the client. type The type of the pipe. fd The file descriptor of the pipe.

Implements Hyperspace::Stream::Gate.

void Hyperspace::Stream::FileReceiverGate::initImpl ( )
overrideprotectedvirtual

Implements the object intialization

When implementing an AsyncInitObject, this method should hold your initialization logic. It is called by AsyncInitObject when needed, and the developer should just reimplement it without invoking it.

Once the initialization procedure is completed, either setReady, setOnePartIsReady or setInitError must be called.

Implements Hemera::AsyncInitObject.

void Hyperspace::Stream::FileReceiverGate::uploadCompleted ( const QByteArray &  id,
const QString &  filePath 
)
signal

Emitted when a file upload is completed.

Listen to this signal to access finished uploads.

id Client id. filePath The full path to the uploaded file