diff --git a/backend/src/file/file.service.ts b/backend/src/file/file.service.ts index 743aeb2..57982c2 100644 --- a/backend/src/file/file.service.ts +++ b/backend/src/file/file.service.ts @@ -5,6 +5,7 @@ import { createReadStream, existsSync } from 'fs'; import { promises as fs } from 'fs'; import { join } from 'path'; import { FileData } from './fileData'; +import { exec } from 'child_process'; @Injectable() export class FileService { @@ -31,9 +32,27 @@ export class FileService { // resolve(stdout); // }); // }); + this.runSampleScript(filePath); return `${filePath}`; } + runSampleScript(filePath: string) { + new Promise((resolve, reject) => { + // Run the shell script and pass the fileName as an argument + exec(`./src/processed/sample.sh sample.jpeg`, (error, stdout, stderr) => { + if (error) { + console.error(`Error: ${error.message}`); + reject(`Error: ${error.message}`); + } else if (stderr) { + console.error(`Standard Error: ${stderr}`); + reject(`Standard Error: ${stderr}`); + } else { + console.log("Script ran succesfully "); + } + }); + }); + } + getImage(imageName: string, res: Response) { const filePath = join( process.cwd(), diff --git a/backend/src/processed/output.json b/backend/src/processed/output.json index 3b22f2b..32960f8 100644 --- a/backend/src/processed/output.json +++ b/backend/src/processed/output.json @@ -1,12 +1,2 @@ [ - { - "imageName":"sample.jpeg", - "downloadUrl":"http://localhost:3000/files/sample-first.jpeg", - "detectionRate":60 - }, - { - "imageName":"sample.jpeg", - "downloadUrl":"http://localhost:3000/files/sample-second.jpeg", - "detectionRate":80 - } ] \ No newline at end of file diff --git a/backend/src/processed/sample.sh b/backend/src/processed/sample.sh new file mode 100755 index 0000000..1ff601c --- /dev/null +++ b/backend/src/processed/sample.sh @@ -0,0 +1,55 @@ +#!/bin/python3 +import json +import sys +import os +import random + +# Check if correct number of arguments are passed +if len(sys.argv) != 2: + print("Expected command: ./sample.sh ") + sys.exit(1) + +# Assign command line argument to variable +file_name = sys.argv[1] + +# Path to the JSON file +file_path = 'src/processed/output.json' + +# Check if output.json exists, if not create a new one with an empty array +if not os.path.exists(file_path): + with open(file_path, 'w') as json_file: + json.dump([], json_file) + +# Read the existing data from output.json +with open(file_path, 'r') as json_file: + data = json.load(json_file) + +# Generate random detection rates between 50 and 100 +detection_rate_first = random.randint(50, 100) +detection_rate_second = random.randint(50, 100) + +# Construct URLs +base_name = file_name.split('.')[0] +url_first = f"http://localhost:3000/files/{base_name}-first.jpeg" +url_second = f"http://localhost:3000/files/{base_name}-second.jpeg" + +# Create new entries +new_entries = [ + { + "fileName": file_name, + "downloadUrl": url_first, + "detectionRate": detection_rate_first + }, + { + "fileName": file_name, + "downloadUrl": url_second, + "detectionRate": detection_rate_second + } +] + +# Append new entries to the existing data +data.extend(new_entries) + +# Write the updated data back to output.json +with open(file_path, 'w') as json_file: + json.dump(data, json_file, indent=4)