ImageObject Save Fails

370
0
08-31-2023 12:20 PM
DavidPuckett
Occasional Contributor

I am converting an image from the HEIC format to jpg in order to upload as an attachment. The ImageObject save method does this but it only returns true or false. I'm getting lots of false returns resulting in a selected image not being available to upload for my users. Is there any way to get more information about why the save operation fails? No errors are thrown and nothing is written to the console. I am checking that the folder where the saved file is going exists and that we have access. The photo sharing setting is set correctly. Am I missing anything here? Thanks for any insights.

 

// set our fileinfo to the file url selected by the user
// so we can do some probing of it
fileInfo.url = fileUrl;
// ios includes some parameters to the url using the conventional ?
// remove them from the suffix so they aren't included in our final file name
let lastIndex = fileInfo.suffix.lastIndexOf('?');
let suffix = (lastIndex !== -1 ? fileInfo.suffix.slice(0, lastIndex) : fileInfo.suffix).toLowerCase();
let picName = "";
let picFileName = "";
// use current date and time as milliseconds as default
// guarantees uniqueness and we could parse out the upload date if needed later
let picDate = Date.parse(new Date());

// load our picture into imageObject in prep for save
imageObject.load(fileInfo.filePath);
// build the file name using the date derived above
// and the original file suffix less any parameters
// and tack on the count if we have multiple
picName = picDate + "." + count;
picFileName = picName + "." + suffix;

// increment count for the next time around
count++;
// make a copy of the selected image
// now create the full destination path by getting the path to the new folder along with our new pic file name
let destination = fileFolder.filePath(picFileName);

// if we have a heic format picture swap the .heic suffix for the .jpg suffix
// *note that this is an undocumented hero act that isn't found in readily available Qt\QML libraries
if (fileUrl.toString().toLowerCase().includes("heic")) {
    destination = destination.replace(".heic", ".jpg");
}

// we are checking for the folder when the app fires and creating it if it doesn't exist
if (fileFolder.exists) {
    // now that the selected image is loaded into imageObject
    // and we have created our destination path
    // call the save method which converts heic to jpg as necessary
    let copied = imageObject.save(destination, "JPG");
    if (copied) {
        // if copied successfully add it to our model and it will be saved when the workflow is saved
        pictureListModel
            .append({
                // this is the path passed to the function to add attachment
                filePath: destination,
                // this is the path we use as the source to display before saving, above
                // windows needs the file:/// prefix others do not
                localFilePath: Qt.platform.os == "windows" ? fileInfo.url.toString() : destination,
                contentType: 'image/jpeg',
                name: picName
            });
    } else {
        app.toast.show("Unable to upload the picture", "fail");
        const details = "The attempt to copy the file failed\n" +
            "we were trying to copy this file: \n" +
            fileInfo.filePath +
            "\nto this location: \n" + destination;
        app.toast.show(details, "fail");
        console.log("we were trying to copy this file", fileInfo.filePath);
        console.log("to this location", destination);
        console.log("copy picture failed");
    }
} else {
    // we have checked for this folder on launch and created it if not there but...
    console.log("The destination folder does not exist");
    app.toast.show("The destination folder does not exist", "fail");
}

 

 

0 Kudos
0 Replies