Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,117 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import subprocess
|
| 4 |
-
from fastapi import FastAPI
|
|
|
|
| 5 |
|
| 6 |
-
app = FastAPI(title="Hitek Archive
|
| 7 |
|
| 8 |
MOUNT_PATH = "/data/"
|
| 9 |
TEST_FILE = "Hitek01.7z"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@app.get("/")
|
| 12 |
async def root():
|
| 13 |
return {
|
| 14 |
-
"message": "🔍 Hitek Archive
|
| 15 |
"developer": "Gopal Parmar",
|
| 16 |
"endpoints": {
|
| 17 |
-
"/
|
| 18 |
-
"/
|
| 19 |
-
"/preview": "Preview first file contents"
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
-
@app.get("/
|
| 24 |
-
async def
|
| 25 |
filepath = os.path.join(MOUNT_PATH, TEST_FILE)
|
| 26 |
if not os.path.exists(filepath):
|
| 27 |
return {"error": f"File {TEST_FILE} not found"}
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
return {
|
| 43 |
-
"developer": "Gopal Parmar",
|
| 44 |
-
"file": TEST_FILE,
|
| 45 |
-
"detected_type": file_type,
|
| 46 |
-
"magic_bytes_hex": header.hex(),
|
| 47 |
-
"file_size_mb": round(os.path.getsize(filepath) / (1024**2), 2)
|
| 48 |
-
}
|
| 49 |
|
| 50 |
-
@app.get("/
|
| 51 |
-
async def
|
| 52 |
filepath = os.path.join(MOUNT_PATH, TEST_FILE)
|
| 53 |
if not os.path.exists(filepath):
|
| 54 |
return {"error": f"File {TEST_FILE} not found"}
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
capture_output=True,
|
| 61 |
-
timeout=120
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
if result.returncode != 0:
|
| 65 |
-
return {
|
| 66 |
-
"developer": "Gopal Parmar",
|
| 67 |
-
"error": "7z extraction failed",
|
| 68 |
-
"stderr": result.stderr.decode()[:500],
|
| 69 |
-
"stdout": result.stdout.decode()[:500]
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
files_found = []
|
| 73 |
-
for root, dirs, files in os.walk(tmp_dir):
|
| 74 |
-
for file in files:
|
| 75 |
-
file_path = os.path.join(root, file)
|
| 76 |
-
size = os.path.getsize(file_path)
|
| 77 |
-
files_found.append({
|
| 78 |
-
"name": file,
|
| 79 |
-
"size_mb": round(size / (1024**2), 2)
|
| 80 |
-
})
|
| 81 |
-
|
| 82 |
-
return {
|
| 83 |
-
"developer": "Gopal Parmar",
|
| 84 |
-
"extracted_files": files_found,
|
| 85 |
-
"total_files": len(files_found)
|
| 86 |
-
}
|
| 87 |
-
except Exception as e:
|
| 88 |
-
return {"error": str(e)}
|
| 89 |
-
|
| 90 |
-
@app.get("/preview")
|
| 91 |
-
async def preview():
|
| 92 |
-
filepath = os.path.join(MOUNT_PATH, TEST_FILE)
|
| 93 |
-
if not os.path.exists(filepath):
|
| 94 |
-
return {"error": f"File {TEST_FILE} not found"}
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
lines = [f.readline().strip() for _ in range(15)]
|
| 116 |
-
return {
|
| 117 |
-
"developer": "Gopal Parmar",
|
| 118 |
-
"file_name": file,
|
| 119 |
-
"first_15_lines": lines
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
return {"error": "No files extracted"}
|
| 123 |
-
except Exception as e:
|
| 124 |
-
return {"error": str(e)}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import tempfile
|
| 4 |
import subprocess
|
| 5 |
+
from fastapi import FastAPI, Query
|
| 6 |
+
import py7zr
|
| 7 |
|
| 8 |
+
app = FastAPI(title="Hitek Archive Extractor", developer="Gopal Parmar")
|
| 9 |
|
| 10 |
MOUNT_PATH = "/data/"
|
| 11 |
TEST_FILE = "Hitek01.7z"
|
| 12 |
|
| 13 |
+
def extract_with_7z(filepath):
|
| 14 |
+
"""Try extracting with system 7z"""
|
| 15 |
+
try:
|
| 16 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 17 |
+
result = subprocess.run(
|
| 18 |
+
["7z", "x", filepath, f"-o{tmp_dir}", "-y"],
|
| 19 |
+
capture_output=True,
|
| 20 |
+
timeout=120
|
| 21 |
+
)
|
| 22 |
+
if result.returncode == 0:
|
| 23 |
+
for root, dirs, files in os.walk(tmp_dir):
|
| 24 |
+
for file in files:
|
| 25 |
+
file_path = os.path.join(root, file)
|
| 26 |
+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 27 |
+
return file, f.read()
|
| 28 |
+
return None, None
|
| 29 |
+
except:
|
| 30 |
+
return None, None
|
| 31 |
+
|
| 32 |
+
def extract_with_py7zr(filepath):
|
| 33 |
+
"""Try extracting with py7zr"""
|
| 34 |
+
try:
|
| 35 |
+
with py7zr.SevenZipFile(filepath, mode='r') as archive:
|
| 36 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 37 |
+
archive.extract(path=tmp_dir)
|
| 38 |
+
for root, dirs, files in os.walk(tmp_dir):
|
| 39 |
+
for file in files:
|
| 40 |
+
file_path = os.path.join(root, file)
|
| 41 |
+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 42 |
+
return file, f.read()
|
| 43 |
+
except:
|
| 44 |
+
pass
|
| 45 |
+
return None, None
|
| 46 |
+
|
| 47 |
@app.get("/")
|
| 48 |
async def root():
|
| 49 |
return {
|
| 50 |
+
"message": "🔍 Hitek Archive Extractor",
|
| 51 |
"developer": "Gopal Parmar",
|
| 52 |
"endpoints": {
|
| 53 |
+
"/extract": "Extract and show content",
|
| 54 |
+
"/search?q=9999999998": "Search in extracted JSON"
|
|
|
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
+
@app.get("/extract")
|
| 59 |
+
async def extract():
|
| 60 |
filepath = os.path.join(MOUNT_PATH, TEST_FILE)
|
| 61 |
if not os.path.exists(filepath):
|
| 62 |
return {"error": f"File {TEST_FILE} not found"}
|
| 63 |
|
| 64 |
+
# Try 7z first
|
| 65 |
+
filename, content = extract_with_7z(filepath)
|
| 66 |
+
if filename:
|
| 67 |
+
return {
|
| 68 |
+
"developer": "Gopal Parmar",
|
| 69 |
+
"method": "7z",
|
| 70 |
+
"file_name": filename,
|
| 71 |
+
"content_preview": content[:500],
|
| 72 |
+
"content_length": len(content)
|
| 73 |
+
}
|
| 74 |
|
| 75 |
+
# Try py7zr
|
| 76 |
+
filename, content = extract_with_py7zr(filepath)
|
| 77 |
+
if filename:
|
| 78 |
+
return {
|
| 79 |
+
"developer": "Gopal Parmar",
|
| 80 |
+
"method": "py7zr",
|
| 81 |
+
"file_name": filename,
|
| 82 |
+
"content_preview": content[:500],
|
| 83 |
+
"content_length": len(content)
|
| 84 |
+
}
|
| 85 |
|
| 86 |
+
return {"error": "Extraction failed with both methods"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
@app.get("/search")
|
| 89 |
+
async def search(q: str = Query(..., min_length=4), limit: int = 10):
|
| 90 |
filepath = os.path.join(MOUNT_PATH, TEST_FILE)
|
| 91 |
if not os.path.exists(filepath):
|
| 92 |
return {"error": f"File {TEST_FILE} not found"}
|
| 93 |
|
| 94 |
+
# Try extract and search
|
| 95 |
+
filename, content = extract_with_7z(filepath)
|
| 96 |
+
if not filename:
|
| 97 |
+
filename, content = extract_with_py7zr(filepath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
if not filename:
|
| 100 |
+
return {"error": "Extraction failed"}
|
| 101 |
+
|
| 102 |
+
results = []
|
| 103 |
+
for line in content.split('\n'):
|
| 104 |
+
if q in line:
|
| 105 |
+
try:
|
| 106 |
+
results.append(json.loads(line))
|
| 107 |
+
except:
|
| 108 |
+
results.append({"raw": line.strip()})
|
| 109 |
+
if len(results) >= limit:
|
| 110 |
+
break
|
| 111 |
+
|
| 112 |
+
return {
|
| 113 |
+
"developer": "Gopal Parmar",
|
| 114 |
+
"query": q,
|
| 115 |
+
"count": len(results),
|
| 116 |
+
"results": results
|
| 117 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|