Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions backend/src/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand Down
10 changes: 0 additions & 10 deletions backend/src/processed/output.json
Original file line number Diff line number Diff line change
@@ -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
}
]
55 changes: 55 additions & 0 deletions backend/src/processed/sample.sh
Original file line number Diff line number Diff line change
@@ -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 <fileName>")
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)