Azeez98 commited on
Commit
a20f7ac
·
verified ·
1 Parent(s): 0517614

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -561
app.py CHANGED
@@ -2,10 +2,11 @@ import os
2
  import subprocess
3
  import tarfile
4
  from fastapi import FastAPI, HTTPException, Form, UploadFile, File, Depends, Query, Response
5
- from fastapi.responses import HTMLResponse, FileResponse
6
  from pydantic import BaseModel
7
  from tempfile import NamedTemporaryFile
8
  from typing import List
 
9
 
10
  app = FastAPI()
11
 
@@ -15,374 +16,7 @@ class DockerImageParams(BaseModel):
15
  image_name: str
16
  tag: str = 'latest'
17
 
18
- # def download_docker_image(image_name, tag='latest', destination='/tmp/docker-images'):
19
- # try:
20
- # os.makedirs(destination, exist_ok=True)
21
-
22
- # tar_path = os.path.join(destination, f'{image_name.replace("/", "_")}.tar')
23
-
24
- # # Remove the existing tar file if it exists
25
- # if os.path.exists(tar_path):
26
- # os.remove(tar_path)
27
-
28
-
29
- # # Skopeo command to copy the image as a tar file
30
- # command = [
31
- # 'skopeo', 'copy',
32
- # f'docker://docker.io/{image_name}:{tag}',
33
- # f'docker-archive:{tar_path}'
34
- # ]
35
- # subprocess.run(command, check=True)
36
- # print(f"Image '{image_name}:{tag}' downloaded successfully to {destination}.")
37
- # return tar_path
38
- # except subprocess.CalledProcessError as e:
39
- # print(f"Error downloading image: {str(e)}")
40
- # raise HTTPException(status_code=500, detail=f"Error downloading Docker image: {str(e)}")
41
-
42
- # @app.get("/", response_class=HTMLResponse)
43
- # async def read_root():
44
- # html_content = """
45
- # <html>
46
- # <head>
47
- # <title>Welcome to FastAPI</title>
48
- # <script>
49
- # function handleDockerFormSubmit(event) {
50
- # event.preventDefault(); // Prevent the form from submitting the traditional way
51
- # const submitButton = document.getElementById('docker-submit-button');
52
- # const messageDiv = document.getElementById('docker-message');
53
- # submitButton.disabled = true; // Disable the submit button
54
- # messageDiv.textContent = "Downloading Docker image..."; // Show downloading message
55
- # messageDiv.style.display = 'block';
56
-
57
- # const form = event.target;
58
- # const imageName = form.image_name.value;
59
- # const tag = form.tag.value;
60
-
61
- # // Start the download
62
- # fetch(`/download-docker-image?image_name=${imageName}&tag=${tag}`)
63
- # .then(response => {
64
- # if (response.ok) {
65
- # return response.blob();
66
- # } else if (response.status === 404) {
67
- # throw new Error('Image not found');
68
- # } else {
69
- # throw new Error('Download failed');
70
- # }
71
- # })
72
- # .then(blob => {
73
- # const url = window.URL.createObjectURL(blob);
74
- # const a = document.createElement('a');
75
- # a.style.display = 'none';
76
- # a.href = url;
77
- # a.download = `${imageName}.tar`;
78
- # document.body.appendChild(a);
79
- # a.click();
80
- # window.URL.revokeObjectURL(url);
81
- # messageDiv.textContent = "Download complete!"; // Show download complete message
82
- # })
83
- # .catch(error => {
84
- # console.error('Error:', error);
85
- # if (error.message === 'Image not found') {
86
- # messageDiv.textContent = "Error: Docker image not found."; // Show not found message
87
- # } else {
88
- # messageDiv.innerHTML = "Error downloading image.<br>Check the image name and tag."; // Show error message in two lines
89
- # }
90
- # })
91
- # .finally(() => {
92
- # submitButton.disabled = false; // Re-enable the submit button after download
93
- # });
94
- # }
95
-
96
- # function handlePipFormSubmit(event) {
97
- # event.preventDefault(); // Prevent the form from submitting the traditional way
98
- # const submitButton = document.getElementById('pip-submit-button');
99
- # const messageDiv = document.getElementById('pip-message');
100
- # submitButton.disabled = true; // Disable the submit button
101
- # messageDiv.textContent = "Downloading dependencies..."; // Show downloading message
102
- # messageDiv.style.display = 'block';
103
-
104
- # const formData = new FormData(event.target);
105
- # fetch('/download-dependencies', {
106
- # method: 'POST',
107
- # body: formData
108
- # })
109
- # .then(response => {
110
- # if (response.ok) {
111
- # return response.blob();
112
- # } else {
113
- # throw new Error('Download failed');
114
- # }
115
- # })
116
- # .then(blob => {
117
- # const url = window.URL.createObjectURL(blob);
118
- # const a = document.createElement('a');
119
- # a.style.display = 'none';
120
- # a.href = url;
121
- # a.download = 'dependencies.tar.gz';
122
- # document.body.appendChild(a);
123
- # a.click();
124
- # window.URL.revokeObjectURL(url);
125
- # messageDiv.textContent = "Download complete!"; // Show download complete message
126
- # })
127
- # .catch(error => {
128
- # console.error('Error:', error);
129
- # messageDiv.innerHTML = "Error downloading dependencies.<br>Please try again."; // Show error message in two lines
130
- # })
131
- # .finally(() => {
132
- # submitButton.disabled = false; // Re-enable the submit button after download
133
- # });
134
- # }
135
- # function handleDebFormSubmit(event) {
136
- # event.preventDefault(); // Prevent the form from submitting the traditional way
137
- # const submitButton = document.getElementById('deb-submit-button');
138
- # const messageDiv = document.getElementById('deb-message');
139
- # submitButton.disabled = true; // Disable the submit button
140
- # messageDiv.textContent = "Downloading Debian packages..."; // Show downloading message
141
- # messageDiv.style.display = 'block';
142
-
143
- # const formData = new FormData(event.target);
144
- # fetch('/download-deb-packages', {
145
- # method: 'POST',
146
- # body: formData
147
- # })
148
- # .then(response => {
149
- # if (response.ok) {
150
- # return response.blob();
151
- # } else {
152
- # throw new Error('Download failed');
153
- # }
154
- # })
155
- # .then(blob => {
156
- # const url = window.URL.createObjectURL(blob);
157
- # const a = document.createElement('a');
158
- # a.style.display = 'none';
159
- # a.href = url;
160
- # a.download = 'deb-packages.tar.gz';
161
- # document.body.appendChild(a);
162
- # a.click();
163
- # window.URL.revokeObjectURL(url);
164
- # messageDiv.textContent = "Download complete!"; // Show download complete message
165
- # })
166
- # .catch(error => {
167
- # console.error('Error:', error);
168
- # messageDiv.innerHTML = "Error downloading Debian packages.<br>Please try again."; // Show error message in two lines
169
- # })
170
- # .finally(() => {
171
- # submitButton.disabled = false; // Re-enable the submit button after download
172
- # });
173
- # }
174
- # </script>
175
- # </head>
176
- # <body>
177
- # <h1>Welcome to Azeez's Help Desk :)</h1>
178
-
179
- # <h2>Docker Image Download</h2>
180
- # <form onsubmit="handleDockerFormSubmit(event)">
181
- # <label for="image_name">Docker Image Name:</label>
182
- # <input type="text" id="image_name" name="image_name" required><br><br>
183
-
184
- # <label for="tag">Docker Image Tag:</label>
185
- # <input type="text" id="tag" name="tag" value="latest" required><br><br>
186
-
187
- # <input type="submit" id="docker-submit-button" value="Download Docker Image">
188
- # </form>
189
- # <div id="docker-message" style="display: none; margin-top: 10px;"></div>
190
-
191
- # <h2>Pip Dependencies Download</h2>
192
- # <form onsubmit="handlePipFormSubmit(event)">
193
- # <label for="requirements_file">Requirements File:</label>
194
- # <input type="file" id="requirements_file" name="requirements_file" accept=".txt" required><br><br>
195
-
196
- # <input type="submit" id="pip-submit-button" value="Download Dependencies">
197
- # </form>
198
- # <div id="pip-message" style="display: none; margin-top: 10px;"></div>
199
-
200
- # <h2>Debian Packages Download</h2>
201
- # <form onsubmit="handleDebFormSubmit(event)">
202
- # <label for="deb_packages">Debian Package Names (comma-separated):</label>
203
- # <input type="text" id="deb_packages" name="deb_packages" required><br><br>
204
-
205
- # <input type="submit" id="deb-submit-button" value="Download Debian Packages">
206
- # </form>
207
- # <div id="deb-message" style="display: none; margin-top: 10px;"></div>
208
-
209
- # </body>
210
- # </html>
211
- # """
212
- # return HTMLResponse(content=html_content)
213
-
214
- # @app.get("/", response_class=HTMLResponse)
215
- # async def read_root():
216
- # html_content = """
217
- # <html>
218
- # <head>
219
- # <title>Welcome to FastAPI</title>
220
- # <script>
221
- # function handleDockerFormSubmit(event) {
222
- # event.preventDefault(); // Prevent the form from submitting the traditional way
223
- # const submitButton = document.getElementById('docker-submit-button');
224
- # const messageDiv = document.getElementById('docker-message');
225
- # submitButton.disabled = true; // Disable the submit button
226
- # messageDiv.textContent = "Downloading Docker image..."; // Show downloading message
227
- # messageDiv.style.display = 'block';
228
-
229
- # const form = event.target;
230
- # const imageName = form.image_name.value;
231
- # const tag = form.tag.value;
232
-
233
- # // Start the download
234
- # fetch(`/download-docker-image?image_name=${imageName}&tag=${tag}`)
235
- # .then(response => {
236
- # if (response.ok) {
237
- # return response.blob();
238
- # } else if (response.status === 404) {
239
- # throw new Error('Image not found');
240
- # } else {
241
- # throw new Error('Download failed');
242
- # }
243
- # })
244
- # .then(blob => {
245
- # const url = window.URL.createObjectURL(blob);
246
- # const a = document.createElement('a');
247
- # a.style.display = 'none';
248
- # a.href = url;
249
- # a.download = `${imageName.replace("/", "_")}.tar`;
250
- # document.body.appendChild(a);
251
- # a.click();
252
- # window.URL.revokeObjectURL(url);
253
- # messageDiv.textContent = "Download complete!"; // Show download complete message
254
- # })
255
- # .catch(error => {
256
- # console.error('Error:', error);
257
- # if (error.message === 'Image not found') {
258
- # messageDiv.textContent = "Error: Docker image not found."; // Show not found message
259
- # } else {
260
- # messageDiv.innerHTML = "Error downloading image.<br>Check the image name and tag."; // Show error message in two lines
261
- # }
262
- # })
263
- # .finally(() => {
264
- # submitButton.disabled = false; // Re-enable the submit button after download
265
- # });
266
- # }
267
-
268
- # function handlePipFormSubmit(event) {
269
- # event.preventDefault(); // Prevent the form from submitting the traditional way
270
- # const submitButton = document.getElementById('pip-submit-button');
271
- # const messageDiv = document.getElementById('pip-message');
272
- # submitButton.disabled = true; // Disable the submit button
273
- # messageDiv.textContent = "Downloading dependencies..."; // Show downloading message
274
- # messageDiv.style.display = 'block';
275
-
276
- # const formData = new FormData(event.target);
277
- # fetch('/download-dependencies', {
278
- # method: 'POST',
279
- # body: formData
280
- # })
281
- # .then(response => {
282
- # if (response.ok) {
283
- # return response.blob();
284
- # } else {
285
- # throw new Error('Download failed');
286
- # }
287
- # })
288
- # .then(blob => {
289
- # const url = window.URL.createObjectURL(blob);
290
- # const a = document.createElement('a');
291
- # a.style.display = 'none';
292
- # a.href = url;
293
- # a.download = 'dependencies.tar.gz';
294
- # document.body.appendChild(a);
295
- # a.click();
296
- # window.URL.revokeObjectURL(url);
297
- # messageDiv.textContent = "Download complete!"; // Show download complete message
298
- # })
299
- # .catch(error => {
300
- # console.error('Error:', error);
301
- # messageDiv.innerHTML = "Error downloading dependencies.<br>Please try again."; // Show error message in two lines
302
- # })
303
- # .finally(() => {
304
- # submitButton.disabled = false; // Re-enable the submit button after download
305
- # });
306
- # }
307
- # function handleDebFormSubmit(event) {
308
- # event.preventDefault(); // Prevent the form from submitting the traditional way
309
- # const submitButton = document.getElementById('deb-submit-button');
310
- # const messageDiv = document.getElementById('deb-message');
311
- # submitButton.disabled = true; // Disable the submit button
312
- # messageDiv.textContent = "Downloading Debian packages..."; // Show downloading message
313
- # messageDiv.style.display = 'block';
314
-
315
- # const formData = new FormData(event.target);
316
- # fetch('/download-deb-packages', {
317
- # method: 'POST',
318
- # body: formData
319
- # })
320
- # .then(response => {
321
- # if (response.ok) {
322
- # return response.blob();
323
- # } else {
324
- # throw new Error('Download failed');
325
- # }
326
- # })
327
- # .then(blob => {
328
- # const url = window.URL.createObjectURL(blob);
329
- # const a = document.createElement('a');
330
- # a.style.display = 'none';
331
- # a.href = url;
332
- # a.download = 'deb-packages.tar.gz';
333
- # document.body.appendChild(a);
334
- # a.click();
335
- # window.URL.revokeObjectURL(url);
336
- # messageDiv.textContent = "Download complete!"; // Show download complete message
337
- # })
338
- # .catch(error => {
339
- # console.error('Error:', error);
340
- # messageDiv.innerHTML = "Error downloading Debian packages.<br>Please try again."; // Show error message in two lines
341
- # })
342
- # .finally(() => {
343
- # submitButton.disabled = false; // Re-enable the submit button after download
344
- # });
345
- # }
346
- # </script>
347
- # </head>
348
- # <body>
349
- # <h1>Welcome to Azeez's Help Desk :)</h1>
350
-
351
- # <h2>Docker Image Download</h2>
352
- # <form onsubmit="handleDockerFormSubmit(event)">
353
- # <label for="image_name">Docker Image Name:</label>
354
- # <input type="text" id="image_name" name="image_name" required><br><br>
355
-
356
- # <label for="tag">Docker Image Tag:</label>
357
- # <input type="text" id="tag" name="tag" value="latest" required><br><br>
358
-
359
- # <input type="submit" id="docker-submit-button" value="Download Docker Image">
360
- # </form>
361
- # <div id="docker-message" style="display: none; margin-top: 10px;"></div>
362
-
363
- # <h2>Pip Dependencies Download</h2>
364
- # <form onsubmit="handlePipFormSubmit(event)">
365
- # <label for="requirements_file">Requirements File:</label>
366
- # <input type="file" id="requirements_file" name="requirements_file" accept=".txt" required><br><br>
367
-
368
- # <input type="submit" id="pip-submit-button" value="Download Dependencies">
369
- # </form>
370
- # <div id="pip-message" style="display: none; margin-top: 10px;"></div>
371
-
372
- # <h2>Debian Packages Download</h2>
373
- # <form onsubmit="handleDebFormSubmit(event)">
374
- # <label for="deb_packages">Debian Package Names (comma-separated):</label>
375
- # <input type="text" id="deb_packages" name="deb_packages" required><br><br>
376
-
377
- # <input type="submit" id="deb-submit-button" value="Download Debian Packages">
378
- # </form>
379
- # <div id="deb-message" style="display: none; margin-top: 10px;"></div>
380
-
381
- # </body>
382
- # </html>
383
- # """
384
- # return HTMLResponse(content=html_content)
385
-
386
  @app.post("/download-dependencies")
387
  async def download_dependencies(requirements_file: UploadFile = File(...)):
388
  try:
@@ -419,36 +53,6 @@ async def download_dependencies(requirements_file: UploadFile = File(...)):
419
  raise HTTPException(status_code=500, detail=str(e))
420
  finally:
421
  os.remove(tmp_path)
422
-
423
- # @app.get("/download-docker-image")
424
- # async def download_image(params: DockerImageParams = Depends()):
425
- # try:
426
- # destination = '/tmp/docker-images'
427
- # tar_path = download_docker_image(params.image_name, params.tag, destination)
428
-
429
- # # Serve the tar file directly
430
- # return FileResponse(tar_path, filename=f'{params.image_name}.tar')
431
-
432
- # except subprocess.CalledProcessError as e:
433
- # error_message = e.stderr.decode('utf-8')
434
- # print(f"Error downloading image: {error_message}")
435
- # if 'manifest unknown' in error_message or 'not found' in error_message:
436
- # raise HTTPException(status_code=404, detail=f"Docker image {image_name}:{tag} not found.")
437
- # else:
438
- # raise HTTPException(status_code=500, detail=f"Error downloading Docker image: {str(e)}")
439
-
440
- # @app.get("/download-docker-image")
441
- # async def download_docker_image_endpoint(image_name: str = Query(...), tag: str = Query('latest')):
442
- # tar_path = download_docker_image(image_name, tag)
443
- # return FileResponse(tar_path, media_type='application/x-tar', filename=f'{image_name.replace("/", "_")}.tar')
444
-
445
- import os
446
- import subprocess
447
- from fastapi import FastAPI, HTTPException, Query
448
- from fastapi.responses import StreamingResponse, HTMLResponse
449
- from pydantic import BaseModel
450
-
451
- app = FastAPI()
452
 
453
  class DockerImageParams(BaseModel):
454
  image_name: str
@@ -477,11 +81,6 @@ def download_docker_image(image_name, tag='latest', destination='/tmp/docker-ima
477
  print(f"Error downloading image: {str(e)}")
478
  raise HTTPException(status_code=500, detail=f"Error downloading Docker image: {str(e)}")
479
 
480
- from fastapi import FastAPI
481
- from fastapi.responses import HTMLResponse
482
-
483
- app = FastAPI()
484
-
485
  @app.get("/", response_class=HTMLResponse)
486
  async def read_root():
487
  html_content = """
@@ -759,123 +358,6 @@ async def read_root():
759
  """
760
  return HTMLResponse(content=html_content)
761
 
762
-
763
- # @app.get("/", response_class=HTMLResponse)
764
- # async def read_root():
765
- # html_content = """
766
- # <html>
767
- # <head>
768
- # <title>Welcome to FastAPI</title>
769
- # <style>
770
- # #progress-container {
771
- # width: 100%;
772
- # background-color: #f3f3f3;
773
- # }
774
- # #progress-bar {
775
- # width: 0%;
776
- # height: 30px;
777
- # background-color: #4caf50;
778
- # text-align: center;
779
- # line-height: 30px;
780
- # color: white;
781
- # }
782
- # </style>
783
- # <script>
784
- # async function handleDockerFormSubmit(event) {
785
- # event.preventDefault(); // Prevent the form from submitting the traditional way
786
- # const submitButton = document.getElementById('docker-submit-button');
787
- # const messageDiv = document.getElementById('docker-message');
788
- # const progressBar = document.getElementById('progress-bar');
789
- # submitButton.disabled = true; // Disable the submit button
790
- # messageDiv.textContent = "Downloading Docker image..."; // Show downloading message
791
- # messageDiv.style.display = 'block';
792
-
793
- # const form = event.target;
794
- # const imageName = form.image_name.value;
795
- # const tag = form.tag.value;
796
-
797
- # try {
798
- # const response = await fetch(`/download-docker-image?image_name=${imageName}&tag=${tag}`);
799
- # if (response.ok) {
800
- # const reader = response.body.getReader();
801
- # const contentLength = response.headers.get('content-length');
802
- # const total = parseInt(contentLength, 10);
803
- # let loaded = 0;
804
-
805
- # const stream = new ReadableStream({
806
- # start(controller) {
807
- # function push() {
808
- # reader.read().then(({ done, value }) => {
809
- # if (done) {
810
- # controller.close();
811
- # progressBar.style.width = '100%';
812
- # messageDiv.textContent = "Download complete!";
813
- # return;
814
- # }
815
- # loaded += value.length;
816
- # const percent = Math.round((loaded / total) * 100);
817
- # progressBar.style.width = `${percent}%`;
818
- # progressBar.textContent = `${percent}%`;
819
- # controller.enqueue(value);
820
- # push();
821
- # }).catch(error => {
822
- # console.error('Error:', error);
823
- # messageDiv.textContent = "Error downloading image.";
824
- # });
825
- # }
826
- # push();
827
- # }
828
- # });
829
-
830
- # const blob = await new Response(stream).blob();
831
- # const url = window.URL.createObjectURL(blob);
832
- # const a = document.createElement('a');
833
- # a.style.display = 'none';
834
- # a.href = url;
835
- # a.download = `${imageName.replace("/", "_")}.tar`;
836
- # document.body.appendChild(a);
837
- # a.click();
838
- # window.URL.revokeObjectURL(url);
839
- # } else if (response.status === 404) {
840
- # throw new Error('Image not found');
841
- # } else {
842
- # throw new Error('Download failed');
843
- # }
844
- # } catch (error) {
845
- # console.error('Error:', error);
846
- # if (error.message === 'Image not found') {
847
- # messageDiv.textContent = "Error: Docker image not found."; // Show not found message
848
- # } else {
849
- # messageDiv.innerHTML = "Error downloading image.<br>Check the image name and tag."; // Show error message in two lines
850
- # }
851
- # } finally {
852
- # submitButton.disabled = false; // Re-enable the submit button after download
853
- # }
854
- # }
855
- # </script>
856
- # </head>
857
- # <body>
858
- # <h1>Welcome to Azeez's Help Desk :)</h1>
859
-
860
- # <h2>Docker Image Download</h2>
861
- # <form onsubmit="handleDockerFormSubmit(event)">
862
- # <label for="image_name">Docker Image Name:</label>
863
- # <input type="text" id="image_name" name="image_name" required><br><br>
864
-
865
- # <label for="tag">Docker Image Tag:</label>
866
- # <input type="text" id="tag" name="tag" value="latest" required><br><br>
867
-
868
- # <input type="submit" id="docker-submit-button" value="Download Docker Image">
869
- # </form>
870
- # <div id="docker-message" style="display: none; margin-top: 10px;"></div>
871
- # <div id="progress-container">
872
- # <div id="progress-bar">0%</</div>
873
- # </div>
874
- # </body>
875
- # </html>
876
- # """
877
- # return HTMLResponse(content=html_content)
878
-
879
  @app.get("/download-docker-image")
880
  async def download_docker_image_endpoint(image_name: str = Query(...), tag: str = Query('latest')):
881
  tar_path = download_docker_image(image_name, tag)
@@ -893,8 +375,6 @@ async def download_docker_image_endpoint(image_name: str = Query(...), tag: str
893
 
894
  return StreamingResponse(iterfile(), media_type='application/x-tar', headers=headers)
895
 
896
-
897
-
898
  def create_tar_file(files_to_package: List[str], tar_filename: str, destination_dir: str):
899
  """
900
  Create a tar file containing specified files.
@@ -963,44 +443,6 @@ def download_make_package(destination_dir):
963
  except subprocess.CalledProcessError as e:
964
  print(f"Error downloading packages: {e}")
965
 
966
- # @app.post("/download-deb-packages")
967
- # async def download_deb_packages_handler(deb_packages: str = Form(...)):
968
- # """
969
- # Endpoint to handle POST requests for downloading Debian packages.
970
- # Args:
971
- # - deb_packages (str): Comma-separated list of Debian package names.
972
- # Returns:
973
- # - FileResponse: Response containing the packaged tar file of downloaded Debian packages.
974
- # """
975
- # try:
976
- # # Parse the list of package names from the input
977
- # package_names = [pkg.strip() for pkg in deb_packages.split(',')]
978
-
979
- # # Example usage:
980
- # download_make_package('/tmp/downloaded_packages')
981
-
982
- # # Define the directory where packages will be stored
983
- # destination_dir = '/tmp/downloaded_packages'
984
-
985
- # # Create the directory if it does not exist
986
- # os.makedirs(destination_dir, exist_ok=True)
987
-
988
- # # Download Debian packages to the destination directory
989
- # downloaded_packages = download_deb_packages(package_names, destination_dir)
990
-
991
- # # Define the name of the tar file
992
- # tar_filename = 'deb-packages.tar.gz'
993
-
994
- # # Create a tar file containing the downloaded packages
995
- # tar_path = create_tar_file(downloaded_packages, tar_filename, destination_dir)
996
-
997
- # # Return the tar file as a response
998
- # return FileResponse(tar_path, filename=tar_filename)
999
-
1000
- # except Exception as e:
1001
- # # Raise HTTPException with a 500 status code if an error occurs
1002
- # raise HTTPException(status_code=500, detail=f"Failed to download Debian packages: {str(e)}")
1003
-
1004
  @app.post("/download-deb-packages")
1005
  async def download_deb_packages_handler(deb_packages: str = Form(...)):
1006
  try:
 
2
  import subprocess
3
  import tarfile
4
  from fastapi import FastAPI, HTTPException, Form, UploadFile, File, Depends, Query, Response
5
+ from fastapi.responses import HTMLResponse, FileResponse, StreamingResponse
6
  from pydantic import BaseModel
7
  from tempfile import NamedTemporaryFile
8
  from typing import List
9
+ from pydantic import BaseModel
10
 
11
  app = FastAPI()
12
 
 
16
  image_name: str
17
  tag: str = 'latest'
18
 
19
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @app.post("/download-dependencies")
21
  async def download_dependencies(requirements_file: UploadFile = File(...)):
22
  try:
 
53
  raise HTTPException(status_code=500, detail=str(e))
54
  finally:
55
  os.remove(tmp_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  class DockerImageParams(BaseModel):
58
  image_name: str
 
81
  print(f"Error downloading image: {str(e)}")
82
  raise HTTPException(status_code=500, detail=f"Error downloading Docker image: {str(e)}")
83
 
 
 
 
 
 
84
  @app.get("/", response_class=HTMLResponse)
85
  async def read_root():
86
  html_content = """
 
358
  """
359
  return HTMLResponse(content=html_content)
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  @app.get("/download-docker-image")
362
  async def download_docker_image_endpoint(image_name: str = Query(...), tag: str = Query('latest')):
363
  tar_path = download_docker_image(image_name, tag)
 
375
 
376
  return StreamingResponse(iterfile(), media_type='application/x-tar', headers=headers)
377
 
 
 
378
  def create_tar_file(files_to_package: List[str], tar_filename: str, destination_dir: str):
379
  """
380
  Create a tar file containing specified files.
 
443
  except subprocess.CalledProcessError as e:
444
  print(f"Error downloading packages: {e}")
445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  @app.post("/download-deb-packages")
447
  async def download_deb_packages_handler(deb_packages: str = Form(...)):
448
  try: