Before Upload Worker Code Sample

JFrog Platform Administration Documentation

Content Type
Administration / Platform
ft:sourceType
Paligo

The following section provides a sample code for an Before Upload worker.

export default async (context: PlatformContext, data: BeforeUploadRequest): Promise<BeforeUploadResponse> => {
    let status: UploadStatus = UploadStatus.UPLOAD_UNSPECIFIED;
    let headers: { [key: string]: Header } = {};

    try {
        // The in-browser HTTP client facilitates making calls to the JFrog REST APIs
        //To call an external endpoint, use 'await context.clients.axios.get("https://foo.com")'
        const res = await context.clients.platformHttp.get('/artifactory/api/v1/system/readiness');

        // You should reach this part if the HTTP request status is successful (HTTP Status 399 or lower)
        if (res.status === 200) {
            status = UploadStatus.UPLOAD_PROCEED;
            headers['Content-Type'] = { value: ['text/plain'] };
            console.log('Artifactory ping success');
        } else {
            status = UploadStatus.UPLOAD_WARN;
            console.warn(`Request was successful but returned status other than 200. Status code : ${res.status}`);
        }
    } catch(error) {
        // The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher
        status = UploadStatus.UPLOAD_STOP;
        console.error(`Request failed with status code ${error.status || '<none>'} caused by : ${error.message}`);
    }

    return {
        status,
        message: 'Overwritten by worker-service if an error occurs.',
        modifiedRepoPath: data.metadata.repoPath,
        headers,
    };
}

Input Parameters

context

Provides baseUrl, token, and clients to communicate with the JFrog Platform (for more information, see PlatformContext).

data

The request with upload details sent by Artifactory.

{
  "metadata": {  // Object containing metadata information about the artifact
    "repoPath": {  // Repository path information for the artifact
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfoder/my-file",  // Current path to the specific artifact within the repository
      "id": "local-repo:folder/subfoder/my-file",  // Unique identifier combining the repository key and path
      "isRoot": false,  // Indicates if the path is a root directory (false means it is nested)
      "isFolder": false  // Indicates if the path is a folder (false means it is a file)
    },
    "contentLength": 100,  // Length of the content in bytes
    "lastModified": 0,  // Timestamp of the last modification (0 indicates it has not been modified)
    "trustServerChecksums": false,  // Indicates whether server checksums should be trusted for validation
    "servletContextUrl": "servlet.com",  // URL for accessing the servlet context
    "skipJarIndexing": false,  // Indicates whether to skip indexing for JAR files (false means indexing will occur)
    "disableRedirect": false,  // Indicates whether HTTP redirects should be disabled
    "repoType": 1  // Numeric identifier representing the type of repository (e.g., local = 1)
  },
  "headers": {  // Object containing HTTP headers associated with the request
    "key": {  // Example of a custom header
      "key": "bla",  // Name of the custom header
      "value": "bla"  // Value of the custom header
    }
  },
  "userContext": {  // Object containing information about the user making the request
    "id": "jffe@00xxxxxxxxxxxxxxxxxxxxxxxx/users/bob",  // Unique identifier for the user
    "isToken": true,  // Indicates if the user is authenticated using a token (true means they are)
    "realm": "realm"  // Realm for user authentication context
  },
  "artifactProperties": {  // Object containing additional properties associated with the artifact
    "anyProperty": {  // Example of a custom property name
      "value": [  // Array of values for the property
        "anything"  // Example value for the property
      ]
    }
  }
}

Response

{
  "data": {  // Main data object containing the result of the operation
    "status": 1,  // The instruction on how to proceed
    "message": "Overwritten by worker-service if an error occurs.",  // Message to print to the log. If there is an error it will be printed as a warning.
    "modifiedRepoPath": {  // Object containing details about the modified repository path
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfoder/my-file",  // Path to the modified file within the repository
      "id": "local-repo:folder/subfoder/my-file",  // Unique identifier combining the repository key and path
      "isRoot": false,  // Indicates whether the path is a root directory (false means it is nested)
      "isFolder": false  // Indicates whether the path is a folder (false means it is a file)
    },
    "headers": {  // Object containing HTTP headers associated with the operation
      "Content-Type": {  // Content-Type header specifying the type of data being returned
        "value": [  // Array containing values for the Content-Type header
          "text/plain"  // Indicates that the content type of the response is plain text
        ]
      }
    }
  },
  "executionStatus": "STATUS_SUCCESS"  // Overall execution status indicating that the operation was successful
}
  • message and status : These are mandatory fields.

  • requestHeaders and modifiedRepoPath: These are optional fields.

Possible Statuses

  • UploadStatus.UPLOAD_PROCEED - The worker allows to proceed with the upload.

  • UploadStatus.UPLOAD_STOP - The worker forbids upload. Upload will be aborted.

  • UploadStatus.UPLOAD_WARN - The worker allows to proceed with the upload. A warning log with the provided message will be recorded in Artifactory.