Example #1
0
 def extract_images(self):
     """
     Extract images from the content, resize and save them locally if they
     are base64 encoded.
     Saves the list of images into the images list property.
     """
     html = BeautifulSoup(self.content, 'html.parser')
     images = []
     try:
         os.makedirs(self.get_images_path())
     except FileExistsError:
         pass
     for img in html.find_all('img'):
         data = img.get('src')
         if is_base64(data):
             m = hashlib.md5()
             m.update(data.encode('utf-8'))
             img_name = m.hexdigest()
             img_path = '{}/{}'.format(self.get_images_path(), img_name)
             img_url = '{}/{}'.format(self.get_images_url(), img_name)
             save_base64_image(data, img_path, (1000, 800))
             img['src'] = img_url
             images.append(img_url)
         else:
             images.append(data)
     for outdated_image in set(self.images) - set(images):
         os.remove(
             os.path.join(self.get_images_path(),
                          os.path.basename(outdated_image)))
     self.images = images
Example #2
0
def profile_edit_avatar():
    try:
        user = User.objects.get(id=current_user.id)
    except User.DoesNotExist:
        abort(404)
    save_base64_image(request.json['data'],
                      '{}/{}'.format(app.config.get('AVATARS_PATH'),
                                     user.id), (200, 200))
    return user.avatar, 201