Generate .stix file from FileField (FielResource/File) object

We are trying to upload a .stix file (D-Stability) and use it to parse with GeoLib.
If we try it locally it works just fine. But when we try to use a FileField. I can’t find a way to convert the FileResource to a workable local file with a corresponding Path to send to the Geolib:

Any idea what we could do?

    @property
    def d_stability_file(self) -> Path:
        file: FileResource = self.get_param("d_stability_file")

        new_file = file.file.from_url(file.file.source)
        location = Path(__file__).parent / file.filename
        with new_file.open() as r:  # open in text-mode, so read -> str
            new_file = File(path=location, data=r.read())

        return Path(new_file.source)

We fixed it

@property
def d_stability_file(self) -> Path:
    file: FileResource = self.get_param("d_stability_file")

    temporary_file = NamedTemporaryFile(suffix='.stix', delete=False, mode='wb')
    temporary_file.write(file.file.getvalue_binary())
    temporary_file.close()

    return Path(temporary_file.name)

This should work I think, which doesn’t require creating a temporary file explicitly:

@property
def d_stability_file(self) -> Path:
    file_resource: FileResource = self.get_param("d_stability_file")
    return file_resource.file.copy().source  # copies the File.from_url to disk (as a File.from_path) and return the path

It does make a local copy, but it looks like it’s not possible to get all the information out of all the nested json files within the stix file using this option.

Error message:

Couldn't find required file at C:\Users\CA0AE~1.RAS\AppData\Local\Temp\tmpijua6k58\soilcorrelations.json

I suppose this is caused by the missing ‘.stix’ extension. The created temporary file with .copy() has no extension. Renaming the path might work:

@property
def d_stability_file(self) -> Path:
    file_resource: FileResource = self.get_param("d_stability_file")
    path = file_resource.file.copy().source  # copies the File.from_url to disk (as a File.from_path)
    new_path = path + '.stix'
    os.rename(path, new_path)
    return new_path