@@ -85,4 +85,42 @@ def download_from_url():
8585with open (tarpath , "wb" ) as f :
8686 f .write (response .raw .read ())
8787
88- shutil .unpack_archive (tarpath , base_dir ) # $result=BAD
88+ shutil .unpack_archive (tarpath , base_dir ) # $result=BAD
89+
90+ # the django upload functionality
91+ # see HttpRequest.FILES: https://docs.djangoproject.com/en/4.1/ref/request-response/#django.http.HttpRequest.FILES
92+ from django .shortcuts import render
93+ from django .core .files .storage import FileSystemStorage
94+ import shutil
95+
96+ def simple_upload (request ):
97+
98+ base_dir = "/tmp/baase_dir"
99+ if request .method == 'POST' :
100+ # Read uploaded files by chunks of data
101+ # see chunks(): https://docs.djangoproject.com/en/4.1/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks
102+ savepath = os .path .join (base_dir , "tarball_compressed.tar.gz" )
103+ with open (savepath , 'wb+' ) as wfile :
104+ for chunk in request .FILES ["ufile1" ].chunks ():
105+ wfile .write (chunk )
106+ shutil .unpack_archive (savepath , base_dir ) # $result=BAD
107+
108+ # Write in binary the uploaded tarball
109+ myfile = request .FILES .get ("ufile1" )
110+ file_path = os .path .join (base_dir , "tarball.tar" )
111+ with file_path .open ('wb' ) as f :
112+ f .write (myfile .read ())
113+ shutil .unpack_archive (file_path , base_dir ) # $result=BAD
114+
115+ # Save uploaded files using FileSystemStorage Django API
116+ # see FileSystemStorage: https://docs.djangoproject.com/en/4.1/ref/files/storage/#django.core.files.storage.FileSystemStorage
117+ for ufile in request .FILES .getlist ():
118+ fs = FileSystemStorage ()
119+ filename = fs .save (ufile .name , ufile )
120+ uploaded_file_path = fs .path (filename )
121+ shutil .unpack_archive (uploaded_file_path , base_dir ) # $result=BAD
122+
123+ return render (request , 'simple_upload.html' )
124+
125+ elif request .method == 'GET' :
126+ return render (request , 'simple_upload.html' )
0 commit comments