示例#1
0
    def post(self, request):
        try:
            result = {}
            path = request.POST["source_path"]
            session_id = request.POST["session_id"]
            user_id = get_user_from_session(session_id).id
            dest_path = request.POST["dest_path"]
            dest_path = dest_path.split("/")
            dest_path.append(str(user_id))
            dest_path[-1], dest_path[-2] = dest_path[-2], dest_path[-1]
            dest_path = "/".join(dest_path)
            print "dest_path at starting is ", dest_path
            try:
                # Increment the name of new directory by one
                directories = map(int, os.listdir(dest_path))
                dest_path += str(max(directories) + 1)
            except:
                # If no directory exists then give it name 1
                dest_path += "/" + "1"

            if dest_path[-1] != "/":
                # Append forward slash to given url if not exists
                dest_path += "/"
            if not os.path.isdir(dest_path):
                os.makedirs(dest_path)
            if path.split(":")[0].lower() == "s3":
                print "[DOWNLOAD API FOR S3]"
                bucket = path.split(":")[1][2:]
                source_path = str(path.split(":")[2])
                result = get_data_from_s3(request, source_path, dest_path, bucket, user_id)

            elif path.split(":")[0].lower() == "dropbox":
                print "[DOWNLOAD API FOR DROPBOX]"
                source_path = path.split(":")[1][1:]
                access_token = SocialToken.objects.get(account__user__id=user_id, app__name="Dropbox")
                session = DropboxSession(settings.DROPBOX_APP_KEY, settings.DROPBOX_APP_SECRET)
                access_key, access_secret = (
                    access_token.token,
                    access_token.token_secret,
                )  # Previously obtained OAuth 1 credentials
                session.set_token(access_key, access_secret)
                client = DropboxClient(session)
                token = client.create_oauth2_access_token()
                print "########### ok ###########"
                result = get_data_from_dropbox(request, source_path, dest_path, token, user_id)
            elif path.split(":")[0].lower() == "gdrive":
                # NON FUNCTIONAL
                get_data_from_google(request, path, access_token)
            else:
                shutil.rmtree(dest_path[:-1])
                result = {
                    "error": "Check if you have attached the type of cloud storage with your account or enter valid path"
                }
        except:
            shutil.rmtree(dest_path[:-1])
            result = {"error": "Invalid Input Provided"}
        print result
        return HttpResponse(json.dumps(result))
 def __init__(self, token, rootPath):
     BaseDrive.__init__(self, token, rootPath)
     APP_KEY = '5a91csqjtsujuw7'
     APP_SECRET = 'x5wbkk2o273jqz7'
     session = DropboxSession(APP_KEY, APP_SECRET)
     print token
     access_key, access_secret = token.split(',')
     session.set_token(access_key, access_secret)
     first_client = DropboxClient(session)
     token1 = first_client.create_oauth2_access_token()
     self.client = DropboxClient(token1)
示例#3
0
    def post(self, request):
        source_path = request.POST["source_path"]
        path = request.POST["dest_path"]
        session_id = request.POST["session_id"]
        user_id = get_user_from_session(session_id).id
        try:
            if path.split(":")[0].lower() == "s3":
                print "[UPLOAD API FOR S3]"
                bucket = path.split(":")[1][2:]
                dest_path = str(path.split(":")[2])
                result = post_data_on_s3(request, source_path, dest_path, bucket, user_id)

            elif path.split(":")[0].lower() == "dropbox":
                print "[UPLOAD API FOR DROPBOX]"
                dest_path = path.split(":")[1][1:]
                access_token = SocialToken.objects.get(account__user__id=user_id, app__name="Dropbox")
                session = DropboxSession(settings.DROPBOX_APP_KEY, settings.DROPBOX_APP_SECRET)
                access_key, access_secret = (
                    access_token.token,
                    access_token.token_secret,
                )  # Previously obtained OAuth 1 credentials
                session.set_token(access_key, access_secret)
                client = DropboxClient(session)
                token = client.create_oauth2_access_token()
                result = post_data_on_dropbox(request, source_path, dest_path, token, user_id)

            elif path.split(":")[0].lower() == "gdrive":
                """*** NOT WORKING ***"""
                print "[UPLOAD API FOR GOOGLE DRIVE]"
                storage = Storage(SocialToken, "id", user_id, "token")
                credential = storage.get()
                # credentials = SocialToken.objects.get(account__user__id = request.user.id, app__name = storage)
                # credentials = credentials.token
                http = credential.authorize(httplib2.Http())
                service = discovery.build("drive", "v2", http=http)
                results = service.files().list(maxResults=10).execute()
                items = results.get("items", [])
                if not items:
                    # print 'No files found.'
                    pass
                else:
                    # print 'Files:'
                    for item in items:
                        print "{0} ({1})".format(item["title"], item["id"])
                result = put_data_on_google_drive(request, path, access_token.token)

            else:
                result = {
                    "error": "Check if you have attached the type of cloud storage with your account or enter valid path"
                }
        except:
            result = {"error": "Incorrect Input"}
        return HttpResponse(json.dumps(result))