@realitydefender/realitydefender
Version:
SDK for the Reality Defender API for deepfake detection
67 lines (66 loc) • 2.15 kB
JavaScript
;
/**
* Media upload functionality
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSignedUrl = getSignedUrl;
exports.uploadToSignedUrl = uploadToSignedUrl;
exports.uploadFile = uploadFile;
const constants_1 = require("../core/constants");
const files_1 = require("../utils/files");
const errors_1 = require("../errors");
/**
* Get a signed URL for uploading a file
* @param client HTTP client
* @param fileName Name of the file to upload
* @returns Signed URL response
*/
async function getSignedUrl(client, fileName) {
try {
return await client.post(constants_1.API_PATHS.SIGNED_URL, { fileName });
}
catch (error) {
if (error instanceof errors_1.RealityDefenderError) {
throw error;
}
throw new errors_1.RealityDefenderError(`Failed to get signed URL: ${error.message}`, 'unknown_error');
}
}
/**
* Upload file content to a signed URL
* @param client HTTP client
* @param signedUrl URL for uploading
* @param filePath Path to the file to upload
*/
async function uploadToSignedUrl(client, signedUrl, filePath) {
try {
const fileContent = (0, files_1.readFileContent)(filePath);
await client.put(signedUrl, fileContent);
}
catch (error) {
if (error instanceof errors_1.RealityDefenderError) {
throw error;
}
throw new errors_1.RealityDefenderError(`Upload failed: ${error.message}`, 'upload_failed');
}
}
/**
* Upload a file for analysis
* @param client HTTP client
* @param options Upload options
* @returns Upload result with request and media IDs
*/
async function uploadFile(client, options) {
const { filePath } = options;
// Get the filename
const fileName = (0, files_1.getFileName)(filePath);
// Get signed URL
const signedUrlResponse = await getSignedUrl(client, fileName);
// Upload to signed URL
await uploadToSignedUrl(client, signedUrlResponse.response.signedUrl, filePath);
// Return result for tracking
return {
requestId: signedUrlResponse.requestId,
mediaId: signedUrlResponse.mediaId,
};
}