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



On Wed, Jun 7, 2017 at 4:57 AM Michael Teuscher <mk teuscher gmail com> wrote:
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...

Hi,

Yes, you are asking in the right place!

However, it will be hard to answer exactly, unless you either 1) specify which line is throwing the exception, or 2) share the whole program so we can run it for ourselves.

That said, it sounds like maybe a libsoup function is incorrectly annotated to return a UTF-8 string when it should be returning a byte array?

Regards,
Philip C 


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