diff --git a/pyigloo/__init__.py b/pyigloo/__init__.py index e5adf3b..e5bddda 100644 --- a/pyigloo/__init__.py +++ b/pyigloo/__init__.py @@ -9,6 +9,8 @@ >>> """ +import os +import pathlib class igloo: @@ -558,3 +560,58 @@ def user_get (self, userId): url = '{0}{1}/User/{2}/Get'.format(self.endpoint, self.IGLOO_API_ROOT_V2, userId) result = self.igloo.get(url) return result.json() + + def post_community_attachment(self, payload): + """ + APIv2 .api2/api/v1/communities/{communityKey}/attachments + + Creates community attachment entry + """ + url = '{0}{1}v1/communities/{2}/attachments'.format(self.endpoint, self.IGLOO_API_ROOT_V2, self.communityKey) + result = self.igloo.post(url, data=payload) + return result.headers['Location'] + + def send_image_to_url(self, url, image, contentType): + """ + APIv1 .api2/api/v1/communities/{communityKey}/attachments/uploads/{attachmentKey} + + Uploads attachment to entry provided from previous call + """ + image = pathlib.Path(image).read_bytes() + + contentLength = len(image) + contentRange = "bytes 0-"+str(contentLength-1)+"/"+str(contentLength) + + headers = {"Content-Range": contentRange, "Content-Length": str(contentLength), "Content-Type": contentType} + + url = '{0}{1}'.format(self.endpoint, url) + result = self.igloo.patch(url, headers=headers, data=image) + return result + + def associate_attachment_to_user(self, attachmentKey, userId): + """ + APIv1 .api2/api/v1/communities/{communityKey}/attachments/{attachmentKey}/association/{objectId}?type=ProfilePhoto + + Tags attachment to user + """ + url = '{0}{1}v1/communities/{2}/attachments/{3}/association/{4}?type=ProfilePhoto'.format(self.endpoint, self.IGLOO_API_ROOT_V2, self.communityKey, attachmentKey, userId) + result = self.igloo.post(url) + return result + + def update_profile_picture(self, userId, filePath, contentType): + """ + Calls the below methods in order to upload a profile picture to user + post_community_attachment + send_image_to_url + associate_attachment_to_user + """ + contentLength = os.path.getsize(filePath) + fileName = os.path.basename(filePath) + + payload = {"name": fileName, "contentType": contentType, "contentLength": contentLength} + location = self.post_community_attachment(payload) + + self.send_image_to_url(location[1:], filePath, contentType) + + attachmentKey = location.split('/')[-1] + self.associate_attachment_to_user(attachmentKey, userId) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6d3327c..1bf499f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ requests>=2.8.1 +python-magic==0.4.27 +python-dotenv==0.20.0 \ No newline at end of file diff --git a/tests/uploadUserProfilePic.py b/tests/uploadUserProfilePic.py new file mode 100644 index 0000000..cabc03c --- /dev/null +++ b/tests/uploadUserProfilePic.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + +import sys +import os +import pathlib +import magic + + +from dotenv import load_dotenv +if os.getenv("ENV") != None: + load_dotenv('.env.' + os.getenv("ENV")) + +load_dotenv() + +try: + sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +except NameError: + sys.path.insert(0, "../") + +import pyigloo + + +params = { + "ACCESS_KEY": os.getenv("ACCESS_KEY"), + "API_KEY": os.getenv("API_KEY"), + "API_USER": os.getenv("API_USER"), + "API_PASSWORD": os.getenv("API_PASSWORD"), + "API_ENDPOINT": os.getenv("API_ENDPOINT") +} + +import argparse +parser = argparse.ArgumentParser() +parser.add_argument("--community_key", help="Community Key") +parser.add_argument("--user_id", help="User ID") +parser.add_argument("--file", help="Path to the image") +parser.add_argument("--content_type", help="Content Type of the image", required=False) +args = parser.parse_args() + +igloo = pyigloo.igloo(params) +igloo.communityKey = args.community_key + +allowedContentType = ['image/png', 'image/jpg'] + +if args.content_type is None: + args.content_type = magic.detect_from_filename(args.file).mime_type + +if args.content_type not in allowedContentType: + print("Allowed contentTypes "+str(" ".join(allowedContentType))) + + +igloo.update_profile_picture(args.user_id, args.file, args.content_type) \ No newline at end of file