-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
141 lines (117 loc) · 3.66 KB
/
Copy pathserver.py
File metadata and controls
141 lines (117 loc) · 3.66 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""
Neocities Manager - Web UI Server
Lightweight Flask server for the Neocities Manager UI
"""
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import os
import sys
import json
from pathlib import Path
import tempfile
# Import the neocities manager functions
import importlib.util
spec = importlib.util.spec_from_file_location("neocities_manager", "neocities-manager.py")
neocities = importlib.util.module_from_spec(spec)
spec.loader.exec_module(neocities)
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
"""Serve the UI"""
return send_file('ui.html')
@app.route('/list', methods=['GET'])
def list_files():
"""List files on Neocities site"""
try:
path = request.args.get('path', '')
# Suppress console output
import io
import contextlib
with contextlib.redirect_stdout(io.StringIO()):
files = neocities.list_files(path)
return jsonify({
'success': True,
'files': files
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/upload', methods=['POST'])
def upload_files():
"""Upload files to Neocities"""
try:
if not request.files:
return jsonify({
'success': False,
'error': 'No files provided'
}), 400
# Create temp directory for uploaded files
temp_dir = tempfile.mkdtemp()
file_map = {}
try:
# Save uploaded files temporarily
for remote_name, file in request.files.items():
temp_path = os.path.join(temp_dir, os.path.basename(remote_name))
file.save(temp_path)
file_map[remote_name] = temp_path
# Upload to Neocities (suppress console output)
import io
import contextlib
with contextlib.redirect_stdout(io.StringIO()):
result = neocities.upload_files(file_map)
return jsonify({
'success': True,
'message': f'Successfully uploaded {len(file_map)} file(s)'
})
finally:
# Clean up temp files
import shutil
try:
shutil.rmtree(temp_dir)
except:
pass
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/delete', methods=['POST'])
def delete_files():
"""Delete files from Neocities"""
try:
data = request.get_json()
files = data.get('files', [])
if not files:
return jsonify({
'success': False,
'error': 'No files specified'
}), 400
# Suppress console output
import io
import contextlib
with contextlib.redirect_stdout(io.StringIO()):
result = neocities.delete_files(files)
return jsonify({
'success': True,
'message': f'Successfully deleted {len(files)} file(s)'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
if __name__ == '__main__':
print('🌐 Neocities Manager Server')
print('=' * 50)
print('Server starting on http://localhost:8080')
print('Open your browser and navigate to:')
print(' → http://localhost:8080')
print('')
print('Press Ctrl+C to stop the server')
print('=' * 50)
print('')
app.run(host='0.0.0.0', port=8080, debug=False)