def test_image_rotation_180(self): from max.utils.image import rotate_image_by_EXIF from PIL import Image image = Image.open('{}/truita2.jpg'.format(self.conf_dir)) rotated = rotate_image_by_EXIF(image) self.assertNotEqual(image, rotated)
def test_image_rotation_no_rotation(self): from max.utils.image import rotate_image_by_EXIF from PIL import Image image = Image.open('{}/avatar.png'.format(self.conf_dir)) rotated = rotate_image_by_EXIF(image) self.assertEqual(image, rotated)
def process_file(self, request, activity_file): """ Process file and save it into the database """ base_path = request.registry.settings.get("file_repository") uploadURL = "" file_type = self["object"].get("objectType", "file").lower() # Look if the activity belongs to an specific context if self.get("contexts", False): # Look if we need to upload to an external URL context = self.get("contexts")[0] ContextClass = getMaxModelByObjectType(self.get("contexts")[0]["objectType"]) identifier = ( context[ContextClass.unique] if ContextClass.unique in context else context[ContextClass.unique.lstrip("_")] ) context = ContextClass.from_database(self.request, identifier) uploadURL = context.get("uploadURL", "") if uploadURL: headers = { "X-Oauth-Scope": request.headers.get("X-Oauth-Scope"), "X-Oauth-Username": request.headers.get("X-Oauth-Username"), "X-Oauth-Token": request.headers.get("X-Oauth-Token"), } # Todo: Make sure that the mimetype is correctly or even informed # Use magic or imghdr libraries for that files = {"image": (activity_file.filename, activity_file.file, activity_file.type)} res = requests.post(uploadURL, headers=headers, files=files) if res.status_code == 201: response = json.loads(res.text) self["object"]["fullURL"] = response.get("uploadURL", "") if file_type == "image": self["object"]["thumbURL"] = response.get("thumbURL", "") else: # We have a conversation or an activity with no related context # or a context with no community which we should save localy dirs = list(re.search("(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{12})", str(self["_id"])).groups()) filename = dirs.pop() path = base_path + "/" + "/".join(dirs) if not os.path.exists(path): os.makedirs(path) r_file = open(path + "/" + filename, "w") r_file.write(activity_file.file.read()) r_file.close() full_endpoint_name = "image/full" if file_type == "image" else "file/download" self["object"]["fullURL"] = "/{}/{}/{}".format(self.resource_root, str(self["_id"]), full_endpoint_name) if file_type == "image": # Generate thumbnail activity_file.file.seek(0) image = Image.open(activity_file.file) thumb = rotate_image_by_EXIF(image) thumb.thumbnail((400, 400), Image.ANTIALIAS) thumb.save(path + "/" + filename + ".thumb", "JPEG") self["object"]["thumbURL"] = "/{}/{}/image/thumb".format(self.resource_root, str(self["_id"])) self["object"]["mimetype"] = activity_file.headers.getheader("content-type", "") # If there's a user-provided filename, preserve it if "filename" not in self["object"]: # Try to get filename from content disposition filename = re.search( r'filename="(.*?)"', activity_file.headers.getheader("content-disposition", "") ).groups() # Ignore empty filenames and "file" filenames (the latter coming from maxclient) if filename and filename != "file": self["object"]["filename"] = filename[0]