Search in sources :

Example 1 with CompleteFileUploadRequest

use of ai.nightfall.scan.model.CompleteFileUploadRequest in project nightfall-java-sdk by nightfallai.

the class NightfallClient method scanFile.

/**
 * A convenience method that abstracts the details of the multi-step file upload and scan process. In other words,
 * calling this method for a given file is equivalent to (1) manually initializing a file upload session,
 * (2) uploading all chunks of the file, (3) completing the upload, and (4) triggering a scan of the file.
 *
 * <p>The maximum allowed <code>contentSizeBytes</code> is dependent on the terms of your current
 * Nightfall usage plan agreement; check the Nightfall dashboard for more details.
 *
 * <p>This method consumes the provided <code>InputStream</code>, but it *does not* close it; closing remains
 * the caller's responsibility.
 *
 * @param request contains configuration describing which detectors to use to scan the file, as well as a webhook
 *                URL for delivering the results of the scan.
 * @param content a stream of the bytes representing the file to upload
 * @param contentSizeBytes the size of the input stream
 * @param timeout the allowed duration for the request; if the execution time exceeds this duration, the request
 *                will be aborted.
 * @return an acknowledgment that the asynchronous scan has been initiated.
 * @throws NightfallAPIException thrown if a non-2xx status code is returned by the API.
 * @throws NightfallClientException thrown if a I/O error occurs while processing the request
 * @throws NightfallRequestTimeoutException thrown if execution time exceeds the provided <code>timeout</code>,
 *      or if an HTTP request is terminated by the client for exceeding read/write timeouts.
 */
public ScanFileResponse scanFile(ScanFileRequest request, InputStream content, long contentSizeBytes, Duration timeout) {
    if (request == null) {
        throw new IllegalArgumentException("request must be non-null");
    } else if (content == null) {
        throw new IllegalArgumentException("content must be non-null");
    }
    Instant deadline = null;
    if (timeout != null) {
        if (timeout.isNegative()) {
            throw new IllegalArgumentException("timeout must be positive");
        }
        deadline = Instant.now().plus(timeout);
    }
    InitializeFileUploadRequest initRequest = new InitializeFileUploadRequest(contentSizeBytes);
    FileUpload upload = this.initializeFileUpload(initRequest);
    AtomicReference<BaseNightfallException> uploadException = new AtomicReference<>();
    boolean uploadSuccess = doChunkedUpload(upload, content, deadline, uploadException);
    if (!uploadSuccess) {
        BaseNightfallException except = uploadException.get();
        if (except != null) {
            throw except;
        }
        throw new NightfallClientException("internal error: failed to upload all chunks of file");
    }
    CompleteFileUploadRequest completeReq = new CompleteFileUploadRequest(upload.getFileID());
    upload = this.completeFileUpload(completeReq);
    return this.scanUploadedFile(request, upload.getFileID());
}
Also used : InitializeFileUploadRequest(ai.nightfall.scan.model.InitializeFileUploadRequest) NightfallClientException(ai.nightfall.scan.model.NightfallClientException) Instant(java.time.Instant) BaseNightfallException(ai.nightfall.scan.model.BaseNightfallException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompleteFileUploadRequest(ai.nightfall.scan.model.CompleteFileUploadRequest) FileUpload(ai.nightfall.scan.model.FileUpload)

Aggregations

BaseNightfallException (ai.nightfall.scan.model.BaseNightfallException)1 CompleteFileUploadRequest (ai.nightfall.scan.model.CompleteFileUploadRequest)1 FileUpload (ai.nightfall.scan.model.FileUpload)1 InitializeFileUploadRequest (ai.nightfall.scan.model.InitializeFileUploadRequest)1 NightfallClientException (ai.nightfall.scan.model.NightfallClientException)1 Instant (java.time.Instant)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1