Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pyigloo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
>>>

"""
import os
import pathlib

class igloo:

Expand Down Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        file_size = os.path.getsize(file_path)
        headers = {
            "Content-Length": f"\"{file_size}\"",
            "Content-Range": f"bytes 0-{file_size-1}/{file_size}"
        }
        response = requests.put(
            url, open(file_path, "rb"), headers=headers
        )

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)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
requests>=2.8.1
python-magic==0.4.27
python-dotenv==0.20.0
51 changes: 51 additions & 0 deletions tests/uploadUserProfilePic.py
Original file line number Diff line number Diff line change
@@ -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)