def validate_file(self, replace_diff_ext=False): def possible_extensions(filename): possible_type = guess_type(filename)[0] if not possible_type: return [] return guess_all_extensions(guess_type(filename)[0]) if not self.mime_type: raise BadMediaFileException(_("Did not process a mime type!")) base_type = self.mime_type.split('/')[0] if base_type not in self.valid_base_types(): raise BadMediaFileException( _("Not a valid %s file.") % self.media_class.get_nice_name().lower()) if self.file_ext.lower() not in possible_extensions(self.form_path): raise BadMediaFileException( _("File {name} has an incorrect file type {ext}.").format( name=self.uploaded_file.name, ext=self.file_ext, )) if not replace_diff_ext and self.file_ext.lower( ) != self.orig_ext.lower(): raise BadMediaFileException( _("The file type of {name} of '{ext}' does not match the " "file type of the original media file '{orig_ext}'. To change " "file types, please upload directly from the " "Form Builder.").format( name=self.uploaded_file.name, ext=self.file_ext.lower(), orig_ext=self.orig_ext.lower(), ))
def validate_file(self, replace_diff_ext=False): if not self.mime_type in self.valid_mime_types(): raise BadMediaFileException(_("Uploaded file is not a ZIP file.")) if not self.uploaded_zip: raise BadMediaFileException(_("There is no ZIP file.")) if self.uploaded_zip.testzip(): raise BadMediaFileException(_("Unable to extract the ZIP file."))
def validate_file(self, replace_diff_ext=False): if not self.mime_type in self.valid_mime_types(): raise BadMediaFileException("Your zip file doesn't have a valid mimetype.") if not self.uploaded_zip: raise BadMediaFileException("There is no ZIP file.") if self.uploaded_zip.testzip(): raise BadMediaFileException("The ZIP file provided was bad.")
def uploaded_zip(self): try: self.uploaded_file.file.seek(0) return zipfile.ZipFile(self.uploaded_file) except Exception as e: msg = _("There was an issue processing the zip file you provided. Error: %s") raise BadMediaFileException(msg % e)
def mime_type(self): try: data = self.uploaded_file.file.read() return CommCareMultimedia.get_mime_type( data, filename=self.uploaded_file.name) except Exception as e: raise BadMediaFileException( "There was an error fetching the MIME type of your file. Error: %s" % e)
def get_image_object(cls, data): try: return Image.open(BytesIO(data)) except IOError: raise BadMediaFileException(_('Upload is not a valid image file.'))