Ejemplo n.º 1
0
def _perform_export(user, do_delete):
    hash_object = hashlib.new('sha256')
    hash_object.update(user.username + user.password)
    file_hash = hash_object.hexdigest()

    emails = list(user.confirmed_emails.values_list('email', flat=True))
    openids = list(user.confirmed_openids.values_list('openid', flat=True))

    photos = []
    for photo in user.photos.all():
        photo_details = (photo.filename, photo.format)
        photos.append(photo_details)

    gm_client = libgearman.Client()
    for server in settings.GEARMAN_SERVERS:
        gm_client.add_server(server)

    workload = {
        'do_delete': do_delete,
        'file_hash': file_hash,
        'username': user.username,
        'emails': emails,
        'openids': openids,
        'photos': photos
    }
    gm_client.do_background('exportaccount', json.dumps(workload))

    download_url = settings.EXPORT_FILES_URL + file_hash + '.xml.gz'
    return download_url
Ejemplo n.º 2
0
    def crop(self, dimensions=None, links_to_create=None):
        if path.isfile(settings.USER_FILES_ROOT + self.full_filename()):
            return  # already done, skip

        if not path.isfile(settings.UPLOADED_FILES_ROOT +
                           self.full_filename()):
            return  # source image doesn't exist, can't crop it

        if not links_to_create:
            links_to_create = []

        x = y = w = h = 0
        if dimensions:
            x = dimensions['x']
            y = dimensions['y']
            w = dimensions['w']
            h = dimensions['h']

        # Queue a job for the cropping/resizing gearman worker
        gm_client = libgearman.Client()
        for server in settings.GEARMAN_SERVERS:
            gm_client.add_server(server)

        workload = {
            'file_hash': self.filename,
            'format': self.format,
            'x': x,
            'y': y,
            'w': w,
            'h': h,
            'links': links_to_create
        }
        gm_client.do_background('cropresize', json.dumps(workload))
Ejemplo n.º 3
0
def resized_avatar(email_hash, size):
    resized_filename = '%s/%s/%s' % (settings.AVATAR_ROOT, size, email_hash)

    # If the resized avatar already exists, don't re-generate it
    if not os.path.isfile(resized_filename):
        gm_client = libgearman.Client()
        for server in settings.GEARMAN_SERVERS:
            gm_client.add_server(server)

        workload = {'email_hash': email_hash, 'size': size}
        gm_client.do('resizeavatar', json.dumps(workload))

    resized_img = Image.open(resized_filename)
    return (resized_filename, resized_img.format)
Ejemplo n.º 4
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    gearman_workload = sys.stdin.read()
    params = json.loads(gearman_workload)

    do_delete = params['do_delete']
    file_hash = params['file_hash']
    username = params['username']
    emails = params['emails']
    openids = params['openids']
    photos = params['photos']

    # Validate inputs
    if file_hash and not is_hex(file_hash):
        logger.error('file_hash is not a hexadecimal value')
        return 1
    for photo in photos:
        (photo_filename, photo_format) = photo
        if not is_hex(photo_filename):
            logger.error("photo_filename '%s' is not a hexadecimal value" %
                         photo_filename)
            return 1
        if photo_format != 'jpg' and photo_format != 'png' and photo_format != 'gif':
            logger.error("photo_format '%s' is not recognized" % photo_format)
            return 1

    dest_filename = settings.EXPORT_FILES_ROOT + file_hash + '.xml.gz'
    destination = gzip.open(dest_filename, 'w')
    destination.write(xml_header())
    destination.write(xml_account(username))
    destination.write(xml_list('email', emails))
    destination.write(xml_list('openid', openids))
    destination.write(xml_photos(photos))
    destination.write(xml_footer())
    destination.close()

    if do_delete:  # Delete files on disk
        gm_client = libgearman.Client()
        for server in settings.GEARMAN_SERVERS:
            gm_client.add_server(server)

        for photo in photos:
            (photo_filename, photo_format) = photo
            workload = {'file_hash': photo_filename, 'format': photo_format}
            gm_client.do_background('deletephoto', json.dumps(workload))

    return 0
Ejemplo n.º 5
0
    def delete(self, delete_file_on_disk=True):
        # Remove links to this photo
        for email in self.emails.all():
            email.set_photo(None)
        for openid in self.openids.all():
            openid.set_photo(None)

        if delete_file_on_disk:
            # Queue a job for the photo deletion gearman worker
            gm_client = libgearman.Client()
            for server in settings.GEARMAN_SERVERS:
                gm_client.add_server(server)

            workload = {'file_hash': self.filename, 'format': self.format}
            gm_client.do_background('deletephoto', json.dumps(workload))

        super(Photo, self).delete()
Ejemplo n.º 6
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    gearman_workload = sys.stdin.read()
    params = json.loads(gearman_workload)

    file_hash = params['file_hash']
    file_format = params['format']
    x = int(params['x'])
    y = int(params['y'])
    w = int(params['w'])
    h = int(params['h'])
    links = params['links']

    # Validate inputs
    if not is_hex(file_hash):
        logger.error('file_hash is not a hexadecimal value')
        return 1
    if file_format != 'jpg' and file_format != 'png' and file_format != 'gif':
        logger.error('file_format is not recognized')
        return 1
    if not isinstance(links, list):
        logger.error('links is not a list')
        return 1
    for l in links:
        if not is_hash_pair(l):
            logger.error('links is not a list of hash pairs')
            return 1

    filename = "%s.%s" % (file_hash, file_format)
    return_code = crop(filename, x, y, w, h)
    if return_code != 0:
        return return_code

    gm_client = libgearman.Client()
    for server in settings.GEARMAN_SERVERS:
        gm_client.add_server(server)

    params = {'file_hash': file_hash, 'format': file_format, 'links': links}
    gm_client.do_background('ready2user', json.dumps(params))

    return 0
Ejemplo n.º 7
0
def change_photo(photo, md5_hash, sha256_hash):
    '''
    Change the photo that the given hashes point to by deleting/creating hard links.
    '''
    photo_hash = None
    photo_format = None
    if photo:
        photo_hash = photo.filename
        photo_format = photo.format

    gm_client = libgearman.Client()
    for server in settings.GEARMAN_SERVERS:
        gm_client.add_server(server)

    workload = {
        'photo_hash': photo_hash,
        'photo_format': photo_format,
        'md5_hash': md5_hash,
        'sha256_hash': sha256_hash
    }
    gm_client.do_background('changephoto', json.dumps(workload))
Ejemplo n.º 8
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    gearman_workload = sys.stdin.read()
    params = json.loads(gearman_workload)

    file_hash = params['file_hash']
    file_format = params['format']
    links = params['links']

    # Validate inputs
    if not is_hex(file_hash):
        logger.error('file_hash is not a hexadecimal value')
        return 1
    if file_format != 'jpg' and file_format != 'png' and file_format != 'gif':
        logger.error('file_format is not recognized')
        return 1
    if not isinstance(links, list):
        logger.error('links is not a list')
        return 1
    for l in links:
        if not is_hash_pair(l):
            logger.error('links is not a list of hash pairs')
            return 1

    filename = "%s.%s" % (file_hash, file_format)
    source = settings.READY_FILES_ROOT + filename
    dest = settings.USER_FILES_ROOT + filename

    # Sanity checks
    if os.path.isfile(dest):
        logger.warning('Destination already exists')
        return 0

    if not os.path.isfile(source):
        logger.error('Source file not found')
        return 1

    # Remove from /ready and move to /user
    try:
        shutil.move(source, dest)
    except:
        logger.error('Cannot move file')
        return 2

    # All done, we can delete the original file as uploaded by the user
    uploaded_file = settings.UPLOADED_FILES_ROOT + filename
    delete_if_exists(uploaded_file)

    # Finally, create any links to email hashes that were requested
    gm_client = libgearman.Client()
    for server in settings.GEARMAN_SERVERS:
        gm_client.add_server(server)

    for hashes in links:
        params = {
            'photo_hash': file_hash,
            'photo_format': file_format,
            'md5_hash': hashes[0],
            'sha256_hash': hashes[1]
        }
        gm_client.do_background('changephoto', json.dumps(params))

    return 0