File size: 3,556 Bytes
74a1521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import requests
import shutil
from urllib.parse import urlparse
from flask import Flask, request, jsonify, send_from_directory

app = Flask(__name__)

# Create directories to simulate a file system
if not os.path.exists('images'):
    os.makedirs('images')

if not os.path.exists('files'):
    os.makedirs('files')

# For simulating a terminal environment (virtual file system)
current_directory = 'files'

@app.route('/')
def index():
    return send_from_directory('static', 'index.html')

@app.route('/download_image')
def download_image():
    url = request.args.get('url')
    if not url:
        return jsonify({'success': False, 'message': 'No URL provided.'})

    try:
        response = requests.get(url)
        if response.status_code == 200:
            image_name = os.path.basename(urlparse(url).path)
            image_path = os.path.join('images', image_name)
            with open(image_path, 'wb') as file:
                file.write(response.content)

            file_size = os.path.getsize(image_path)
            file_size = f'{file_size / (1024 * 1024):.2f} MB'  # Convert to MB
            return jsonify({
                'success': True,
                'image_url': f'/images/{image_name}',
                'file_size': file_size,
                'message': f'Image downloaded: {image_name}'
            })
        else:
            return jsonify({'success': False, 'message': 'Failed to fetch image.'})
    except Exception as e:
        return jsonify({'success': False, 'message': str(e)})

@app.route('/images/<filename>')
def serve_image(filename):
    return send_from_directory('images', filename)

@app.route('/terminal', methods=['POST'])
def terminal():
    command = request.json.get('command')
    global current_directory
    output = ""
    
    if command.startswith("ls"):
        output = "\n".join(os.listdir(current_directory)) or "No files or directories."
    elif command.startswith("cat"):
        filename = command.split(" ")[1]
        try:
            with open(os.path.join(current_directory, filename), 'r') as f:
                output = f.read()
        except FileNotFoundError:
            output = f"Error: {filename} not found."
    elif command.startswith("mkdir"):
        folder_name = command.split(" ")[1]
        os.makedirs(os.path.join(current_directory, folder_name), exist_ok=True)
        output = f"Directory {folder_name} created."
    elif command.startswith("touch"):
        filename = command.split(" ")[1]
        with open(os.path.join(current_directory, filename), 'w') as f:
            pass
        output = f"File {filename} created."
    elif command.startswith("rm"):
        filename = command.split(" ")[1]
        try:
            os.remove(os.path.join(current_directory, filename))
            output = f"File {filename} removed."
        except FileNotFoundError:
            output = f"Error: {filename} not found."
    elif command.startswith("cd"):
        folder_name = command.split(" ")[1]
        new_directory = os.path.join(current_directory, folder_name)
        if os.path.isdir(new_directory):
            current_directory = new_directory
            output = f"Changed directory to {folder_name}"
        else:
            output = f"Error: {folder_name} not found."
    elif command == "pwd":
        output = f"Current directory: {current_directory}"
    else:
        output = "Unknown command. Type 'help' for a list of available commands."

    return jsonify({'output': output})

if __name__ == '__main__':
    app.run(debug=True)