Can't create thumbnails from javascript

I’m using this piece of code in DING to generate a thumbnail from a file, but I always receive this error:

(gjs:36972): Gjs-CRITICAL **: 17:27:19.498: JS ERROR: GLib.Error g-spawn-exit-error-quark: Child process exited with code 1
CreateThumbnail@./app/createThumbnail.js:57:49
@./app/createThumbnail.js:67:1

It happens both with GnomeDesktop 3.0 and 4.0, both with sync and async functions. But Nautilus thumbnailer seems to work fine. Also, this code did work before (although for the last versions I had to add a null extra parameter in generate_thumbnail() call).

The code:

#!/usr/bin/gjs

'use strict';
imports.gi.versions.GnomeDesktop = '4.0';
const GnomeDesktop = imports.gi.GnomeDesktop;
const Gio = imports.gi.Gio;

function CreateThumbnail() {
    let thumbnailFactoryNormal = GnomeDesktop.DesktopThumbnailFactory.new(GnomeDesktop.DesktopThumbnailSize.NORMAL);
    let thumbnailFactoryLarge = GnomeDesktop.DesktopThumbnailFactory.new(GnomeDesktop.DesktopThumbnailSize.LARGE);

    let file = Gio.File.new_for_path(ARGV[0]);
    if (!file.query_exists(null)) {
        return 1;
    }

    let fileUri = file.get_uri();
    let fileInfo = file.query_info('standard::content-type,time::modified', Gio.FileQueryInfoFlags.NONE, null);
    let modifiedTime = fileInfo.get_attribute_uint64('time::modified');

    // check if the thumbnail has been already created in the meantime by another program
    let thumbnailLarge = thumbnailFactoryLarge.lookup(fileUri, modifiedTime);
    if (thumbnailLarge != null) {
        return 3;
    }
    let thumbnailNormal = thumbnailFactoryNormal.lookup(fileUri, modifiedTime);
    if (thumbnailNormal != null) {
        return 3;
    }
    if (thumbnailFactoryNormal.has_valid_failed_thumbnail(fileUri, modifiedTime)) {
        return 4;
    }

    // now, generate the file
    print(fileUri)
    print(fileInfo.get_content_type())
    let thumbnailPixbuf = thumbnailFactoryLarge.generate_thumbnail(fileUri, fileInfo.get_content_type(), null);
    if (thumbnailPixbuf == null) {
        thumbnailFactoryLarge.create_failed_thumbnail(fileUri, modifiedTime);
        return 2;
    } else {
        thumbnailFactoryLarge.save_thumbnail(thumbnailPixbuf, fileUri, modifiedTime);
        return 0;
    }
}

CreateThumbnail();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.