Example #1
0
def download_file(key):
    """
    /download/<file_key> ->
        Authorizes download through ESGF
        Checks if the user has asked for this file, and removes them from the list of users
        Transfers file
    """
    if key is None:
        raise ValueError("No key provided")

    with db_session() as s:
        f = s.query(File).filter(File.key == key)
        if not f:
            raise ValueError("File not found.")

        f = f[0]
        if not access_control.check_access(g.user, url= "/%s/%s" % (f.group, f.key)):
            raise ValueError("File not found.")

        for transfer in f.transfers:
            if transfer.progress != 100:
                continue

            # Check if the user is in the notification list
            for notif in transfer.subscribers:
                if notif.email == g.user_email:
                    # TODO: Only delete the user from the notification list if the download is successful.
                    s.delete(notif)
                    break
            s.commit()
            return send_file(filecache.get_file_path(transfer.file.key), as_attachment=True, attachment_filename=transfer.file.file_name())

        raise ValueError("File not ready for download.")
Example #2
0
def queue_job(group, key):
    """
    /queue/<group>/<file_key> ->
        Authenticate Session w/ESGF
        Verify Permissions w/ESGF
        Check File Exists
        Queue in DB
    """
    if key is None:
        raise ValueError("No key provided.")

    with db_session() as s:

        f = s.query(File).filter(File.key == key and File.group == group).all()
        if not f:
            raise ValueError("No file matching key/group exposed.")

        f = f[0]

        # Verify Permissions
        if access_control.check_access(g.user) is False:
            # Should redirect to the registration URL for the relevant group...
            raise ValueError("User does not have access to the requested file.")

        # Check if already queued
        for t in s.query(Transfer).filter(Transfer.file == f):
            if t.progress < 100:
                for sub in t.subscribers:
                    if sub.email == g.user_email:
                        break
                else:
                    notif = Notification(transfer_id=t.id, email=g.user_email)
                    s.add(notif)
                    s.commit()
            else:
                # Redirect to download URL
                return redirect(url_for("download_file", key=key, _external=True))
            break
        else:
            # Queue transfer in DB
            t = Transfer(file=f, progress=0)
            s.add(t)
            s.commit()
            notif = Notification(transfer_id=t.id, email=g.user_email)
            s.add(notif)
            s.commit()

    progress_url = url_for("job_progress", key=key, _external=True)
    return redirect(progress_url)