Пример #1
0
 def transport_file(cls, user, file_url, target_path):
     if not cls.check_settings(user.target_settings):
         return  # Should never reach
     try:
         service = cls.get_drive_client(user.target_settings)
         path_id = cls.find_path(service, user.target_settings["parent_id"], target_path.split("/")[1:-1])
         media_body = apiclient.http.MediaIoBaseUpload(
             BytesIO(ivle.get_file(file_url)), mimetype=get_mime_type(target_path), resumable=True
         )
         body = {"title": target_path[target_path.rfind("/") + 1 :], "parents": [{"id": path_id}]}
         file = service.files().insert(body=body, media_body=media_body).execute()
         return bool(file["id"])
     except client.AccessTokenRefreshError as e:
         raise SyncException(
             "You are not logged in to Google Drive or your token is expired. Please re-login on the webpage.",
             retry=True,
             send_email=True,
             disable_user=True,
             logout_user=True,
         )
     except apiclient.errors.ResumableUploadError as e:
         raise SyncException(
             "ResumableUploadError, will retry.", retry=True, send_email=False, disable_user=False, logout_user=False
         )
     except apiclient.errors.HttpError as e:
         raise SyncException(str(e), retry=True, send_email=False, disable_user=False, logout_user=False)
     except Exception as e:
         raise SyncException(
             "Something might go wrong with your Google Drive settings. If you are not able to find the error, please inform the developer.",
             retry=True,
             send_email=True,
             disable_user=True,
             logout_user=False,
         )
Пример #2
0
    def transport_file(cls, user, file_url, target_path):
        if not cls.check_settings(user.target_settings):
            return  # Should never reach
        try:
            dropbox_client = dropbox.client.DropboxClient(
                user.target_settings['token'])
            file_data = dropbox_client.put_file(
                user.target_settings['folder'] + target_path,
                ivle.get_file(file_url),
                parent_rev=user.target_settings['files_revision'].get(
                    target_path, ''))

            with user.lock:
                user.sync_from_db()
                user.target_settings['files_revision'][
                    target_path] = file_data['revision']
                user.update()
            return True
        except dropbox.rest.ErrorResponse as e:
            if e.status == 401:
                raise SyncException(
                    "You are not logged in to Dropbox or your token is expired. Please re-login on the webpage.",
                    retry=True,
                    send_email=True,
                    disable_user=True,
                    logout_user=True)
            elif e.status in [400, 429, 500, 503]:
                if e.status == 400 and (
                    ("'parent_rev' is not well-formed" in e.error_msg) or
                    ("Invalid parent_rev" in e.error_msg)):
                    with user.lock:
                        user.sync_from_db()
                        user.target_settings['files_revision'][
                            target_path] = ''
                        user.update()
                raise SyncException(e.error_msg,
                                    retry=True,
                                    send_email=False,
                                    disable_user=False,
                                    logout_user=False)
            elif e.status == 507:
                raise SyncException(
                    "Dropbox says you are over quota. We have temporarily disabled syncing for you. Please manually re-enable after cleaning up some files.",
                    retry=True,
                    send_email=True,
                    disable_user=True,
                    logout_user=False)
            raise e
Пример #3
0
 def transport_file(cls, user, file_url, target_path):
     if not cls.check_settings(user.target_settings):
         return  # Should never reach
     try:
         service = cls.get_drive_client(user.target_settings)
         path_id = cls.find_path(service, user.target_settings['parent_id'],
                                 target_path.split('/')[1:-1])
         media_body = apiclient.http.MediaIoBaseUpload(
             BytesIO(ivle.get_file(file_url)),
             mimetype=get_mime_type(target_path),
             resumable=True)
         body = {
             'title': target_path[target_path.rfind('/') + 1:],
             'parents': [{
                 'id': path_id
             }]
         }
         file = service.files().insert(body=body,
                                       media_body=media_body).execute()
         return bool(file['id'])
     except client.AccessTokenRefreshError as e:
         raise SyncException(
             "You are not logged in to Google Drive or your token is expired. Please re-login on the webpage.",
             retry=True,
             send_email=True,
             disable_user=True,
             logout_user=True)
     except apiclient.errors.ResumableUploadError as e:
         raise SyncException("ResumableUploadError, will retry.",
                             retry=True,
                             send_email=False,
                             disable_user=False,
                             logout_user=False)
     except apiclient.errors.HttpError as e:
         raise SyncException(str(e),
                             retry=True,
                             send_email=False,
                             disable_user=False,
                             logout_user=False)
     except Exception as e:
         raise SyncException(
             "Something might go wrong with your Google Drive settings. If you are not able to find the error, please inform the developer.",
             retry=True,
             send_email=True,
             disable_user=True,
             logout_user=False)
Пример #4
0
    def transport_file(cls, user, file_url, target_path):
        if not cls.check_settings(user.target_settings):
            return  # Should never reach
        try:
            dropbox_client = dropbox.client.DropboxClient(user.target_settings["token"])
            file_data = dropbox_client.put_file(
                user.target_settings["folder"] + target_path,
                ivle.get_file(file_url),
                parent_rev=user.target_settings["files_revision"].get(target_path, ""),
            )

            with user.lock:
                user.sync_from_db()
                user.target_settings["files_revision"][target_path] = file_data["revision"]
                user.update()
            return True
        except dropbox.rest.ErrorResponse as e:
            if e.status == 401:
                raise SyncException(
                    "You are not logged in to Dropbox or your token is expired. Please re-login on the webpage.",
                    retry=True,
                    send_email=True,
                    disable_user=True,
                    logout_user=True,
                )
            elif e.status in [400, 429, 500, 503]:
                if e.status == 400 and (
                    ("'parent_rev' is not well-formed" in e.error_msg) or ("Invalid parent_rev" in e.error_msg)
                ):
                    with user.lock:
                        user.sync_from_db()
                        user.target_settings["files_revision"][target_path] = ""
                        user.update()
                raise SyncException(e.error_msg, retry=True, send_email=False, disable_user=False, logout_user=False)
            elif e.status == 507:
                raise SyncException(
                    "Dropbox says you are over quota. We have temporarily disabled syncing for you. Please manually re-enable after cleaning up some files.",
                    retry=True,
                    send_email=True,
                    disable_user=True,
                    logout_user=False,
                )
            raise e
Пример #5
0
 def transport_file(cls, user, file_url, target_path):
     if not cls.check_settings(user.target_settings):
         return  # Should never reach
     try:
         content = ''
         credentials = client.OAuth2Credentials.from_json(user.target_settings['credentials'])
         http_auth = credentials.authorize(httplib2.Http())
         target_path = target_path.replace('\t', '_')  # TODO: Temp workaround for OD bug on \t
         cls.create_path(http_auth, target_path.split('/')[1:-1])
         (resp_headers, content) = http_auth.request("https://api.onedrive.com/v1.0/drive/special/approot:%s:/content" % urllib.parse.quote(target_path),
                                                     method="PUT", body=ivle.get_file(file_url), headers={'content-type': get_mime_type(target_path)})
         if resp_headers['status'] in [str(i) for i in [429, 500, 501, 503]]:
             raise SyncException("HTTP Error: %s" % str(resp_headers), retry=True, send_email=False, disable_user=False, logout_user=False)
         elif resp_headers['status'] == '400':
             raise SyncException("400: %s" % str(resp_headers), retry=True, send_email=False, disable_user=False, logout_user=False)
         elif resp_headers['status'] == '507':
             raise SyncException(
                 "OneDrive says you are over quota. We have temporarily disabled syncing for you. Please manually re-enable after cleaning up some files.",
                 retry=True, send_email=True, disable_user=True, logout_user=False)
         file_id = json.loads(content.decode('ascii'))['id']
         (resp_headers, content) = http_auth.request("https://api.onedrive.com/v1.0/drive/items/%s" % file_id, method="PATCH",
                                                     body=json.dumps({'name': target_path[target_path.rfind('/') + 1:]}),
                                                     headers={'content-type': 'application/json'})
         if resp_headers['status'] in [str(i) for i in [429, 500, 501, 503]]:
             raise SyncException("HTTP Error: %s" % str(resp_headers), retry=True, send_email=False, disable_user=False, logout_user=False)
         elif resp_headers['status'] == '400':
             raise SyncException("400: %s" % str(resp_headers), retry=True, send_email=False, disable_user=False, logout_user=False)
         return bool(json.loads(content.decode('ascii'))['id'])
     except client.AccessTokenRefreshError as e:
         raise SyncException("You are not logged in to OneDrive or your token is expired. Please re-login on the webpage.", retry=True, send_email=True,
                             disable_user=True, logout_user=True)
     except ConnectionResetError as e:
         raise SyncException("Connection reset. Ignoring.", retry=True, send_email=False, disable_user=False, logout_user=False)
     except (KeyError, ValueError) as e:
         raise SyncException("Cannot find. Ignoring. Info: %s" % content, retry=True, send_email=False, disable_user=False, logout_user=False)
     except SyncException as e:
         raise
     except Exception as e:
         raise SyncException("Something might go wrong with your OneDrive settings. If you are not able to find the error, please inform the developer.",
                             retry=True, send_email=True, disable_user=True, logout_user=False)
Пример #6
0
 def transport_file(cls, user, file_url, target_path):
     if not cls.check_settings(user.target_settings):
         return  # Should never reach
     try:
         content = ''
         credentials = client.OAuth2Credentials.from_json(
             user.target_settings['credentials'])
         http_auth = credentials.authorize(httplib2.Http())
         target_path = target_path.replace(
             '\t', '_')  # TODO: Temp workaround for OD bug on \t
         target_path = target_path.replace(
             ':', '_')  # TODO: Temp workaround for OD bug on :
         cls.create_path(http_auth, target_path.split('/')[1:-1])
         (resp_headers, content) = http_auth.request(
             "https://api.onedrive.com/v1.0/drive/special/approot:%s:/content"
             % urllib.parse.quote(target_path),
             method="PUT",
             body=ivle.get_file(file_url),
             headers={'content-type': get_mime_type(target_path)})
         if resp_headers['status'] in [
                 str(i) for i in [429, 500, 501, 503]
         ]:
             raise SyncException("HTTP Error: %s" % str(resp_headers),
                                 retry=True,
                                 send_email=False,
                                 disable_user=False,
                                 logout_user=False)
         elif resp_headers['status'] == '400':
             raise SyncException("400: %s" % str(resp_headers),
                                 retry=True,
                                 send_email=False,
                                 disable_user=False,
                                 logout_user=False)
         elif resp_headers['status'] == '507':
             raise SyncException(
                 "OneDrive says you are over quota. We have temporarily disabled syncing for you. Please manually re-enable after cleaning up some files.",
                 retry=True,
                 send_email=True,
                 disable_user=True,
                 logout_user=False)
         file_id = json.loads(content.decode('ascii'))['id']
         (resp_headers, content) = http_auth.request(
             "https://api.onedrive.com/v1.0/drive/items/%s" % file_id,
             method="PATCH",
             body=json.dumps(
                 {'name': target_path[target_path.rfind('/') + 1:]}),
             headers={'content-type': 'application/json'})
         if resp_headers['status'] in [
                 str(i) for i in [429, 500, 501, 503]
         ]:
             raise SyncException("HTTP Error: %s" % str(resp_headers),
                                 retry=True,
                                 send_email=False,
                                 disable_user=False,
                                 logout_user=False)
         elif resp_headers['status'] == '400':
             raise SyncException("400: %s" % str(resp_headers),
                                 retry=True,
                                 send_email=False,
                                 disable_user=False,
                                 logout_user=False)
         return bool(json.loads(content.decode('ascii'))['id'])
     except client.AccessTokenRefreshError as e:
         raise SyncException(
             "You are not logged in to OneDrive or your token is expired. Please re-login on the webpage.",
             retry=True,
             send_email=True,
             disable_user=True,
             logout_user=True)
     except ConnectionResetError as e:
         raise SyncException("Connection reset. Ignoring.",
                             retry=True,
                             send_email=False,
                             disable_user=False,
                             logout_user=False)
     except (KeyError, ValueError) as e:
         raise SyncException("Cannot find. Ignoring. Info: %s" % content,
                             retry=True,
                             send_email=False,
                             disable_user=False,
                             logout_user=False)
     except SyncException as e:
         raise
     except Exception as e:
         raise SyncException(
             "Something might go wrong with your OneDrive settings. If you are not able to find the error, please inform the developer.",
             retry=True,
             send_email=True,
             disable_user=True,
             logout_user=False)