def save_uploaded_file(source_file: TemporaryUploadedFile): if not path.isdir(settings.FLEIO_TEMP_DIR): mkdir(settings.FLEIO_TEMP_DIR) temp_file_name = path.join(settings.FLEIO_TEMP_DIR, str(uuid4())) with open(temp_file_name, 'wb+') as destination_file: for chunk in source_file.chunks(): destination_file.write(chunk) return temp_file_name
def create_temp_zip(file: TemporaryUploadedFile) -> NamedTemporaryFile: """ reads the uplaoded file into a temporary zip file which will later be used to extract the audio files from :param file: file uploaded by the user :return: the temporary zip file of the uploaded file """ # renaming like this is ok since a pptx file is the hood a zip pptx_temp_file = NamedTemporaryFile(delete=False, suffix='.zip') with pptx_temp_file as destination: for chunk in file.chunks(): destination.write(chunk) pptx_zip = zipfile.ZipFile(pptx_temp_file.name) return pptx_zip
def store_file(name: str, path: str, file: TemporaryUploadedFile): r"""Stores the file sent in a request body. Stores the file sent in the request body with a particular name at a particular place, defined by the `name` and `path` paramteters. Parameters ---------- name : str Name of the file. path : str Path of the storage location. file: FileType object A python wrapper around a file, sent in a request body. Returns ------- None Writes the file to a location. Raises ------ FileNotFoundError When file path doesn't exist. Examples -------- These are written in doctest format, and should illustrate how to use the function. >>> store_file("a.mp4", "", request.FILES['video']) """ file_name = path + name try: with open(file_name, "wb+") as file_writer: for chunk in file.chunks(): file_writer.write(chunk) lg.debug(f"Written file to {file_name}") except Exception as e: lg.error(str(e))