Beispiel #1
0
    def construct(label, data):
        assert label, "The label is not specified!"
        assert data, "The data is not specified!"
        #assert user, "The user is not specified!"

        type = get_file_type(label)
        size = len(data)

        def randomize(name):
            parts = name.split(".")
            pfx = util.rand(9999)
            parts[len(parts) - 2] = "%s_[%s]" % (parts[len(parts) - 2], str(pfx))
            name = ".".join(parts)
            return name

        file_path = None
        label = label.strip().lower()
        while True:
            name = randomize(label) if file_path else label
            bucket = util.rand(1000)
            subbucket = util.rand(1000)
            path = "%s/%s/%s" % (str(bucket), str(subbucket), name)
            file_path = FilePath.resolve(path, type)
            if not file_path.exists:
                directory = os.path.dirname(file_path.path)
                if not os.path.exists(directory):
                    parent = os.path.dirname(directory)
                    if not os.path.exists(parent):
                        os.mkdir(parent)

                    os.mkdir(directory)
                break

        util.io.write_file(
            file_path.path,
            data,
            text=True if type == "text" else False
        )
        f = File.objects.create(
            label=label,
            uri=file_path.relative,
            type=type,
            size=size,
            data=data
        )
        return f
Beispiel #2
0
 def randomize(name):
     parts = name.split(".")
     pfx = util.rand(9999)
     parts[len(parts) - 2] = "%s_[%s]" % (parts[len(parts) - 2], str(pfx))
     name = ".".join(parts)
     return name