The moveFilesToTrash() API is not taking any effect

In this part of my project, I’m trying to create a Text Messaging feature. It’s been going so well but I figured I will eventually run out of Wix Storage.
I want: After a user sends an Image Message to another user, when or if they decide to delete that message, I want the image to be deleted too. Since moving it to trash is my only option, I did that but it doesn’t seem to be moving the image trash. It also doesn’t return any error it just completes but nothing happens. The image just remains there.

Here’s my code:

import { created, ok, notFound, serverError } from 'wix-http-functions';
import { mediaManager } from 'wix-media-backend';

export function put_deleteImage(request) {
    let options = {
        "headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*",
        }
    };
    // get the request body
    return request.body.json()
        .then((body) => {
            let fileUrls = body.imageToDelete //['https://static.wixstatic.com/media/951883_7c8712a0083047bea717caea43af7c26~mv2.jpg']
            return myMoveFilesToTrashFunction(fileUrls)
                .then((deleted) => {
                    options.body = {
                        "success": ""
                    }
                    return ok(options);
                }).catch((err) => {
                    options.body = {
                        "error": err
                    };
                    return serverError(options);
                })
        })
}

export function myMoveFilesToTrashFunction(fileUrls) {
    return mediaManager.moveFilesToTrash(fileUrls)
        .then(() => {
            return
        })
        .catch((error) => {
            console.error(error);
        })
}

PS: I’m sending an http request through XCode Swift.

The only thing I think is going wrong is that The request doesn’t have the authorization to move a file to trash.

Any help would be appreciated by me expecially. Thanks in advance
Brendan O.

1 Like

Based on the provided code, it seems that you’re using the Wix Velo platform and the moveFilesToTrash() API from wix-media-backend to move images to the trash when a message is deleted. If the images are not being moved to the trash as expected, there are a few potential issues to consider:

  1. File URL format: Ensure that the fileUrls you’re passing to myMoveFilesToTrashFunction() are in the correct format. The URLs should be direct links to the images you want to move to the trash. Verify that the URLs are correct and accessible.

  2. Permission and authentication: Make sure that the Velo code has the necessary permissions and authentication to perform the moveFilesToTrash() operation. Check that the code has the appropriate Media permissions and the necessary API keys or authentication tokens.

  3. Error handling: The current code does not provide any specific error handling or logging within the myMoveFilesToTrashFunction(). It’s possible that errors are occurring during the moveFilesToTrash() operation but are not being captured or logged. Modify the code to include proper error handling and logging to help identify any potential issues.

  4. Testing and debugging: Test the code with sample data and monitor the console logs or error messages to see if any relevant information is provided. This can help identify any specific errors or issues that may be occurring.

If the issue persists, I recommend reaching out to the Wix support team or consulting the Wix Velo documentation for further assistance. They will have more in-depth knowledge of the platform and can help troubleshoot the specific problem you’re facing.

The issue with the image not being moved to the trash after using the moveFilesToTrash() API in your Wix Velo code could be due to the function not returning a resolved promise. To fix this, modify your myMoveFilesToTrashFunction as follows:

javascript code
export function myMoveFilesToTrashFunction(fileUrls) {
    return mediaManager.moveFilesToTrash(fileUrls)
        .then(() => {
            return Promise.resolve();
        })
        .catch((error) => {
            console.error(error);
            return Promise.reject(error);
        });
}

By returning a resolved promise (Promise.resolve()) when the files are successfully moved to trash, you indicate that the operation was successful. Similarly, returning a rejected promise (Promise.reject(error)) when an error occurs allows proper error handling.
Remember to ensure that the fileUrls array passed to myMoveFilesToTrashFunction contains valid URLs of the images you want to delete.
If the issue persists, double-check the Wix Media Manager API documentation for any specific requirements or limitations regarding moving files to the trash. You may also consider reaching out to Wix support for further assistance.
Regarding the HTTP request from Xcode Swift, make sure you are correctly sending the request to the put_deleteImage endpoint and handling the response appropriately.
I hope these suggestions help resolve the issue with moving the image to the trash in your Wix Velo project.

Thank you for your repsonse but there was no luck.

For the issue number 2: What kind of permission do I need? the Wix API does not mention anything about permission or authentication tokens.

Brendan O.

I really appreciate your effort but I added the Promise.resolve() and that didn’t change the result.

In the API, they said " Moving many files to trash at once is an asynchronous action. It may take some time for the results to be seen in the Media Manager "

I added async inbetween export and function in attempt to make it an asynchronous function. Yet nothing .

How else can I make this function an asynchronous function? Maybe that could be the solution.
Do you also have an idea how long it can take for the result to be seen in the media maager?

Thank you
Brendan O.

Were you able to resolve this? I am having a similar issue!

If anyone is having the same issue, this is the reply I got from Wix as of Sept 2023:

"It looks like the issue is specifically with passing the image URL as an item for the filesUrl array parameter. Even though the console.log shows image is successfully moved to trash, it’s not. There is an error, although the documentation states to use the image URLs.

Our devs are still investigating the cause of this issue, but we have a workaround at the moment. You can use the src of the image file, and the image should be successfully moved to trash when passed to the filesUrl array parameter. You can obtain the src of the image by just using the file name.

For example, full image URL is: “https://static.wixstatic.com/media/eab983_e68f470b0e844bd8bec112e32b16e6cd~mv2.jpeg

Instead, use: "eab983_e68f470b0e844bd8bec112e32b16e6cd~mv2.jpeg"

Once we have an update regarding the issue with using the full image URL, we will be sure to notify you."

I tested this temporary solution, and it works.

1 Like

thank you, it’s work
i move file .pdf to Trash use only filename from file url.