How to download an image with libsoup and save it to filesystem in gjs



Hello everyone,

I've encountered a problem google is not able to solve.

I am currently trying to download an image with libsoup and then save it with GIO to the filesystem in $HOME/.cache/folder/image.extension

Sadly I always end up with a 'Failed to convert UTF-8 string to JS string: Invalid byte sequence in conversion input' error.

Maybe you guys know what I am doing wrong

Code I use to save my image

image is an MessageBody from libsoup

_saveImageToFile: function (image) {
        let path = GLib.get_home_dir() + "/.cache/spotlight/test.asd";

        let file = Gio.File.new_for_path(path);
        file.replace_async(null, false,
            Gio.FileCreateFlags.REPLACE_DESTINATION,
            GLib.PRIORITY_LOW, null, (file, res) => {

                let stream;
                if (!res.had_error()) {
                    stream = file.replace_finish(res);
                    let bytes = GLib.Bytes.prototype.new_from_bytes(image.data);
                    stream.write_bytes(bytes, null);
                    // Write more data with stream.write ...
                    stream.close(null);
                }
            });


        //let output = fstream.get_output_stream();
        /**let dstream = new Gio.DataOutputStream(fstream);
        for(let byte in image.get_chunk(0).get_as_bytes())
            dstream.put_byte(byte,null);*/
        //fstream.close(null);
        return path;
    }

Code I use for accessing the image

_httpSession.queue_message(request, function(_httpSession, message) {
                if (message.status_code !== 200) {
                    reject(message.status_code);
                    return;
                }
                global.log("Suceeded API Call");
                resolve(request.response_body.data);
            });

I hope this was the right place to ask...

Thanks in advance

Teryx


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]