-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
228 lines (178 loc) · 7.64 KB
/
functions.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import zipfile
import requests
import argparse
import os
from converter import IFCConverter
# Get the directory where the script is located
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
# Directories for uploads and downloads
LOCAL_STORE_DIR = os.path.join(SCRIPT_DIR, "local store")
# Constants for the server URL
SERVER_URL = "http://127.0.0.1:8000"
def upload_files(ifc_file, img_file):
ifc_file_path = ifc_file
img_file_path = img_file
if not os.path.exists(ifc_file_path):
print(f"File '{ifc_file}' not found in '{LOCAL_STORE_DIR}'. Please check the filename.")
return
if not os.path.exists(img_file_path):
print(f"File '{img_file}' not found in '{LOCAL_STORE_DIR}'. Please check the filename.")
url = f"{SERVER_URL}/upload/"
# Open the file in binary mode and prepare it for uploading
with open(ifc_file_path, "rb") as f:
files = {"ifc_file": (ifc_file, f)}
with open(img_file_path, "rb") as im:
files.update({"img_file": (img_file, im)})
# Send POST request to the server
response = requests.post(url, files=files)
# Print the response from the server
if response.status_code == 200:
print("File uploaded successfully.")
else:
print("Failed to upload file.")
def download_folder(folder):
url = f"{SERVER_URL}/download/{folder}"
# Send GET request to the server to download the file
response = requests.get(url)
if response.status_code == 200:
os.makedirs(LOCAL_STORE_DIR, exist_ok=True)
extracted_folder_path = os.path.join(LOCAL_STORE_DIR, folder)
zip_file_path = os.path.join(LOCAL_STORE_DIR, f"{folder}.zip")
# Save the ZIP file locally
with open(zip_file_path, "wb") as zip_file:
for chunk in response.iter_content(chunk_size=8192):
zip_file.write(chunk)
print(f"ZIP file downloaded successfully: {zip_file_path}")
# Extract the ZIP file into a folder
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(extracted_folder_path)
print(f"Files extracted to: {extracted_folder_path}")
# Optionally, delete the ZIP file after extraction
os.remove(zip_file_path)
print(f"ZIP file deleted: {zip_file_path}")
return extracted_folder_path
else:
print(f"Failed to download folder. Status code: {response.status_code}")
print("Response:", response.json())
return None
def list_files():
url = f"{SERVER_URL}/files/"
# Send GET request to list the files from the server
response = requests.get(url)
if response.status_code == 200:
files = response.json().get("files", [])
if files:
print("Files on the server:")
for file in files:
print(f"{file['filename']} - Size: {file['size']} bytes, Created: {file['created_at']}")
else:
print("No files found on the server.")
else:
print(f"Failed to retrieve file list. Status code: {response.status_code}")
print("Response:", response.json())
def convert_file(filename):
"""
Convert a downloaded IFC file to OBJ and XML formats
"""
# Initialize the converter
converter = IFCConverter(LOCAL_STORE_DIR, os.path.join(SCRIPT_DIR, "converted"))
# Perform the conversion
result = converter.convert_file(filename)
if result["status"] == "success":
print(f"File converted successfully!")
print(f"OBJ file: {result['obj_path']}")
print(f"XML file: {result['xml_path']}")
else:
print(f"Conversion failed: {result['message']}")
def delete_file(filename):
"""
Delete a file from the server.
"""
url = f"{SERVER_URL}/delete/{filename}"
print(f"Sending DELETE request for '{filename}' to {url}...")
# Send DELETE request to the server
response = requests.delete(url)
# Handle server responses
if response.status_code == 200:
print(f"File '{filename}' deleted successfully from the server.")
elif response.status_code == 404:
print(f"File '{filename}' not found on the server.")
elif response.status_code == 400:
print(f"Invalid request: {response.json().get('detail', 'Unknown error')}")
else:
print(f"Failed to delete file. Status code: {response.status_code}")
print("Response:", response.text)
def bulk_download():
"""
Download all files from the server.
"""
# Get the list of files from the server
url = f"{SERVER_URL}/files/"
response = requests.get(url)
if response.status_code == 200:
files = response.json().get("files", [])
if not files:
print("No files available on the server to download.")
return
# Ensure the download directory exists
os.makedirs(LOCAL_STORE_DIR, exist_ok=True)
print(f"Found {len(files)} files on the server. Starting bulk download...")
for file_info in files:
filename = file_info["filename"]
print(f"Downloading '{filename}'...")
file_url = f"{SERVER_URL}/download/{filename}"
file_response = requests.get(file_url)
if file_response.status_code == 200:
save_path = os.path.join(LOCAL_STORE_DIR, filename)
with open(save_path, "wb") as f:
f.write(file_response.content)
print(f"Downloaded '{filename}' to '{save_path}'.")
else:
print(f"Failed to download '{filename}'. Status code: {file_response.status_code}")
print("Bulk download completed.")
else:
print("Failed to retrieve file list from the server.")
print(f"Status code: {response.status_code}, Response: {response.json()}")
if __name__ == "__main__":
# Ensure the upload directory exists
os.makedirs(LOCAL_STORE_DIR, exist_ok=True)
# Set up argument parser
parser = argparse.ArgumentParser(description="Upload, Download, or List IFC files")
parser.add_argument("operation",
choices=["upload", "download", "list", "convert", "delete", "bulk_download"],
help="Choose 'upload', 'download', 'list', 'convert', 'delete', or 'bulk_download'")
parser.add_argument("file_name", type=str,
help="File name to upload from uploads folder or download from the server. (For list operation, use 'list')",
nargs='?')
parser.add_argument("img_file", type=str,
nargs='?',)
# Parse arguments
args = parser.parse_args()
if args.operation == "upload":
if args.file_name and args.img_file:
# Upload file from the uploads directory
upload_files(args.file_name, args.img_file)
else:
print("Please provide the filenames to upload.")
elif args.operation == "download":
if not args.file_name:
print("Please provide the filename to download.")
else:
# Download the file from the server with the same name
download_folder(args.file_name)
elif args.operation == "list":
# List the files available on the server
list_files()
elif args.operation == "convert":
if not args.file_name:
print("Please provide the filename to convert.")
else:
# Convert the downloaded file
convert_file(args.file_name)
elif args.operation == "delete":
if not args.file_name:
print("Please provide the filename to delete.")
else:
delete_file(args.file_name)
elif args.operation == "bulk_download":
bulk_download()