OpenClinica / enketo-oc

OpenClinica's fork of the Enketo web forms monorepo
Apache License 2.0
0 stars 5 forks source link

Error retrieving uploaded file after submission #79

Open theywa opened 2 years ago

theywa commented 2 years ago

Describe the bug File upload forms are unable to fetch the uploaded OC-17883

To Reproduce

  1. Create a form with an image upload
  2. Upload an image file
  3. Submit
  4. View the form

Expected behavior The file can be viewed and downloaded

Screenshots Screen Shot 2022-05-02 at 22 46 40

Additional context After some debug I found the error is on getFileUrl() method in file-manager

function getFileUrl(subject) {
    return new Promise((resolve, reject) => {
        if (!subject) {
            resolve(null);
        } else if (typeof subject === 'string') {
            const escapedSubject = encodeURIComponent(subject);

            if (subject.startsWith('/')) {
                resolve(subject);
            } else if (
                instanceAttachments &&
                Object.prototype.hasOwnProperty.call(
                    instanceAttachments,
                    escapedSubject
                )
            ) {
                resolve(instanceAttachments[escapedSubject]);
            } else if (
                instanceAttachments &&
                Object.prototype.hasOwnProperty.call(
                    instanceAttachments,
                    subject
                )
            ) {
                resolve(instanceAttachments[subject]);
            } else if (!settings.offline || !store.available) {
                // e.g. in an online-only edit view
                reject(new Error('store not available'));
            } else if (URL_RE.test(subject)) {
                // Any URL values are default binary values. These should only occur in offline-capable views,
                // because the form cache module removed the src attributes
                // (which are /urls/like/this/http:// and are caught above this statement)
                store.survey.resource
                    .get(settings.enketoId, subject)
                    .then((file) => {
                        if (file.item) {
                            resolve(URL.createObjectURL(file.item));
                        } else {
                            reject(new Error('File Retrieval Error'));
                        }
                    })
                    .catch(reject);
            } else {
                // obtain file from storage
                store.record.file
                    .get(_getInstanceId(), subject)
                    .then((file) => {
                        if (file.item) {
                            if (isTooLarge(file.item)) {
                                reject(_getMaxSizeError());
                            } else {
                                resolve(URL.createObjectURL(file.item));
                            }
                        } else {
                            reject(new Error('File Retrieval Error'));
                        }
                    })
                    .catch(reject);
            }
        } else if (typeof subject === 'object') {
            if (isTooLarge(subject)) {
                reject(_getMaxSizeError());
            } else {
                resolve(URL.createObjectURL(subject));
            }
        } else {
            reject(new Error('Unknown error occurred'));
        }
    });
}

for the screenshot above the value of subject is downloadMediaEncrypted%20(1)-19_16_10.zip then it will go this condition

else if (!settings.offline || !store.available) {
      // e.g. in an online-only edit view
      reject(new Error('store not available'));
}

that's why it error because it can't find the correct path/link of the file. I'm not sure about the correct value for the subject variable, should it be the link of the file or just the file name like right now? @MartijnR do you have any idea about this issue?

MartijnR commented 2 years ago

@theywa, I believe that file should be part of instanceAttachments (it is send with the API request to Enketo). This would be the place to investigate first I think. See oc-api-v1-controller.js to see if the file is correctly received by the API on the Enketo server.

(I see this issue is now been assigned to me though, but I wanted to respond anyway).

MartijnR commented 2 years ago

It would actually be easier to work on by a developer that has access to the OC backend (that sends the attachment).

theywa commented 2 years ago

thank you for the clue @MartijnR, I just created PR and I think this issue is related to the changes in this commit 5d7f22a7(fixed: OC: form media not showing after: (1) submitting record in offline), is there another place that you might think I should check to make sure the code works properly? thank you in advance