ImagingTools Core SDK
Public Member Functions | List of all members
imtcom::IFileTransferabstract

Interface for file upload and download operations via URLs. More...

#include <IFileTransfer.h>

Inherits istd::IPolymorphic.

Inherited by imthttp::CHttpFileTransfer [virtual].

Public Member Functions

virtual bool UploadFile (const QString &filePath, const QUrl &url) const =0
 Upload a local file to a remote URL.
 
virtual bool DownloadFile (const QString &filePath, const QUrl &url) const =0
 Download a remote file to a local path.
 

Detailed Description

Interface for file upload and download operations via URLs.

IFileTransfer provides a simple contract for transferring files to and from remote servers using URL-based addressing. This interface abstracts the underlying transfer protocol (HTTP, FTP, etc.) and provides a uniform API for file operations.

Key Features

Bidirectional Transfer:

Protocol Agnostic:

Synchronous Operations:

Implementation

Primary Implementation:

Usage Examples

File Upload

IFileTransfer* transfer = ...; // e.g., imthttp::CHttpFileTransfer
// Upload local file to server
QString localFile = "/path/to/document.pdf";
QUrl uploadUrl("https://example.com/api/upload");
if (transfer->UploadFile(localFile, uploadUrl)) {
qDebug() << "File uploaded successfully";
} else {
qCritical() << "Upload failed";
// Check logs for detailed error
}
Interface for file upload and download operations via URLs.
virtual bool UploadFile(const QString &filePath, const QUrl &url) const =0
Upload a local file to a remote URL.

File Download

IFileTransfer* transfer = ...;
// Download remote file to local path
QString localPath = "/tmp/downloaded-report.pdf";
QUrl downloadUrl("https://example.com/api/files/report-2024.pdf");
if (transfer->DownloadFile(localPath, downloadUrl)) {
qDebug() << "File downloaded to:" << localPath;
QFile::exists(localPath); // Verify file exists
} else {
qCritical() << "Download failed";
}
virtual bool DownloadFile(const QString &filePath, const QUrl &url) const =0
Download a remote file to a local path.

Document Management System

class DocumentManager : public QObject {
public:
DocumentManager(IFileTransfer* transfer, const QUrl& baseUrl)
: m_transfer(transfer), m_baseUrl(baseUrl) {}
bool UploadDocument(const QString& localPath, const QString& documentId) {
QUrl uploadUrl = m_baseUrl;
uploadUrl.setPath(QString("/documents/%1").arg(documentId));
qInfo() << "Uploading document" << documentId << "from" << localPath;
if (m_transfer->UploadFile(localPath, uploadUrl)) {
qInfo() << "Document uploaded successfully";
emit documentUploaded(documentId);
return true;
} else {
qCritical() << "Failed to upload document";
emit uploadFailed(documentId);
return false;
}
}
bool DownloadDocument(const QString& documentId, const QString& localPath) {
QUrl downloadUrl = m_baseUrl;
downloadUrl.setPath(QString("/documents/%1").arg(documentId));
qInfo() << "Downloading document" << documentId << "to" << localPath;
if (m_transfer->DownloadFile(localPath, downloadUrl)) {
qInfo() << "Document downloaded successfully";
emit documentDownloaded(documentId, localPath);
return true;
} else {
qCritical() << "Failed to download document";
emit downloadFailed(documentId);
return false;
}
}
signals:
void documentUploaded(const QString& documentId);
void documentDownloaded(const QString& documentId, const QString& path);
void uploadFailed(const QString& documentId);
void downloadFailed(const QString& documentId);
private:
IFileTransfer* m_transfer;
QUrl m_baseUrl;
};

Best Practices

  1. Error Handling: Always check return value and log failures
    • Network errors, permission issues, disk full, etc.
    • Provide user feedback for failed operations
  2. File Validation: Verify file exists before upload
    if (!QFile::exists(localPath)) {
    qCritical() << "File not found:" << localPath;
    return false;
    }
    transfer->UploadFile(localPath, url);
  3. URL Construction: Use QUrl for proper URL encoding
    QUrl url("https://example.com/api/upload");
    url.setQuery("token=abc123"); // Proper encoding
  4. Path Validation: Ensure local paths are writable for downloads
    QFileInfo info(localPath);
    if (!info.dir().exists()) {
    info.dir().mkpath("."); // Create directory
    }
  5. Large Files: Consider using separate thread for blocking operations
    QtConcurrent::run([transfer, localPath, url]() {
    return transfer->UploadFile(localPath, url);
    });
  6. Authentication: Ensure URL includes auth tokens or use authenticated client

Limitations

Integration Points

Implementations:

Related Components:

See also
imthttp::CHttpFileTransfer, IServerConnectionInterface

Definition at line 202 of file IFileTransfer.h.

Member Function Documentation

◆ DownloadFile()

virtual bool imtcom::IFileTransfer::DownloadFile ( const QString &  filePath,
const QUrl &  url 
) const
pure virtual

Download a remote file to a local path.

Downloads the file from the specified URL and saves it to the local filesystem. The operation is synchronous and blocks until completion or failure.

Parameters
filePathAbsolute path where the downloaded file should be saved. Parent directory must exist and be writable.
urlRemote URL of the file to download. Typically an HTTP(S) endpoint.
Returns
true if download completed successfully; false on any error.
Example:
QString savePath = "/tmp/report.pdf";
QUrl fileUrl("https://example.com/reports/2024-annual.pdf");
if (transfer->DownloadFile(savePath, fileUrl)) {
qDebug() << "Downloaded to:" << savePath;
// File now available at savePath
} else {
qCritical() << "Download failed";
}
Note
This is a blocking operation. For large files or UI applications, consider executing in a separate thread.
Warning
If the local file already exists, it will be overwritten without warning.
See also
UploadFile()

◆ UploadFile()

virtual bool imtcom::IFileTransfer::UploadFile ( const QString &  filePath,
const QUrl &  url 
) const
pure virtual

Upload a local file to a remote URL.

Uploads the specified local file to the remote server identified by the URL. The operation is synchronous and blocks until completion or failure.

Parameters
filePathAbsolute path to the local file to upload. Must exist and be readable.
urlRemote URL where the file should be uploaded. Typically an HTTP(S) endpoint.
Returns
true if upload completed successfully; false on any error.
Example:
QString file = "/home/user/document.pdf";
QUrl endpoint("https://api.example.com/upload");
if (transfer->UploadFile(file, endpoint)) {
qDebug() << "Upload successful";
} else {
qCritical() << "Upload failed - check logs";
}
Note
This is a blocking operation. For large files or UI applications, consider executing in a separate thread.
See also
DownloadFile()