Esempio n. 1
0
    def PUT(self, request):
        """Save a file in the similar swift struct.

        The path of all object is: ../storage-<port>/account/container/object
        """
        logging.debug(request)
        response = {"headers": [], "status": "", "body": ""}

        if request.path_level > 0:
            path = request.path["storage_path"]
            if not os.path.exists(path):
                os.makedirs(path)

            response["body"] = "Directory created"
            response["status"] = http_code["201"]
            if request.path_level == 3:
                path += "/%s" % (request.path["object"])  # full file path
                try:
                    with open(path, "wb") as f:
                        f.write(request.object_data)
                        response["body"] = "Upload completed"
                        response["status"] = http_code["201"]
                except:
                    response["body"] = "Could not open the file"
                    response["status"] = http_code["500"]
        else:
            response["body"] = "Could not open the file"
            response["status"] = http_code["500"]

        # Inserting metadata and content if necessary
        parse_metadata = StorageDbManager(request.path)
        parse_metadata.insert_data_db(data=request.metadata)

        return response
Esempio n. 2
0
    def DELETE(self, request):
        """Delete some file and your metadata."""
        logging.debug(request)
        response = {"headers": [], "status": "", "body": ""}
        storage_path = request.path["storage_path"]
        delete = False
        if request.path_level < 3:  # isn't an object
            if os.path.exists(storage_path):
                shutil.rmtree(request.path["storage_path"])
                delete = True
            else:
                response.update({"status": http_code["204"]})
                response["body"] = "Content not found."
        elif request.path_level == 3:
            path = "{}/{}".format(request.path["storage_path"], request.path["object"])
            if os.path.isfile(path):
                os.remove(path)
                delete = True
            else:
                response["body"] = "Content not found."
                response.update({"status": http_code["204"]})

        if delete:
            storage_db = StorageDbManager(request.path)
            if storage_db.delete_in_db(request.path, request.path_level):
                response.update({"status": http_code["200"]})
            else:
                response.update({"status": http_code["204"]})

        return response
Esempio n. 3
0
    def HEAD(self, request):
        """Get metada from metadata_db.

        Return a dictionary of dictionaries because the idea is return from
        accounts, containers and object. But, currently only return of objects.
        """
        response = {"headers": [], "status": "", "body": ""}
        parse_metadata = StorageDbManager(request.path)
        query = parse_metadata.get_metadata()
        for i in query:
            keys = i.keys()
            for k in keys:
                response["headers"].append((str(k), str(i[k])))

        response["status"] = http_code["302"]
        return response