Programmatic way to populate Media Gallery column in a Collection

I need a way to:

  1. Upload a bunch of images (thousands)

  2. Programmatically connect those images to a Media Gallery field/column in a Collection

  3. Every row in the database has a few images in a Gallery. So the images I upload need to be inserted into different rows in the database, not one big gallery in one single row.

Basically:
Collection Foo
Foo.ID 0001
Foo.ImageGallery 0001_a.jpg, 0001_b.jpg

Foo.ID 0002
Foo.ImageGallery 0002_a.jpg, 0002_b.jpg

I did this once manually. There were thousands of rows, and thousands of images, and the Wix UI was cumbersome and took FOREVER and I made too many mistakes. Now I need to do this again, and I need a way to do it with code.

Here is what I learned from the first time I did this:

The Media Gallery field in a Collection is stored a JSON blob. I’ve found other threads on this forum that say that you can just create a JSON blob with three required fields, and insert it into the database. Those 3 required fields are:
src
type
slug
Some articles don’t mention slug as required, but some do.

The last time I did all this (manually), I dumped the database, and I see that the Media Gallery column looks like this JSON blob: (an array of ImageItems)

[{
 "description":"",
 "slug":"c565a4_7e985bb77aa54cc2b121e60081056974mv2",
 "src":"wix:image://v1/c565a4_7e985bb77aa54cc2b121e60081056974~mv2.jpg/0001_sjpg#originWidth=600&originHeight=400",
 "title":"",
 "type":"image",
 "settings":[]
 },
 {
 "description":"",
 "slug":"c565a4_96b6054a880f405d89ad0568a10b429dmv2",
 "src":"wix:image://v1/c565a4_96b6054a880f405d89ad0568a10b429d~mv2.jpg/0001_2_sjpg#originWidth=600&originHeight=400",
 "title":"",
 "type":"image",
 "settings":{}
 }]

I can actually bulk upload the images using the Wix Media Manager UI. But once they’re there, I can’t find a straightforward way to programmatically insert them into a collection. If I bulk upload them, I don’t know how I can (later) ask the media manager for them, when all I know is their ORIGINAL filename. The only method I see is getFileInfo, which requires a (Wix) filename. And the only things that return a FileInfo are mediaManager upload/importFile.

Does this mean I have to programmatically upload all of my images, capturing the map between the Wix filename and the original filename? Or is there a way to ask the media manager for files based on their original file name (or a directory listing, and I’ll iterate through all of it).

Assuming there is a way to programmatically get the Wix filename after-the-fact, then the next thing I need to do is create a Gallery blob and then insert that into the appropriate row in my Collection.

For that, I need 2 bits of information.
src
slug

I have a map between every image filename and the unique Collection ID they belong to. So I also need the original filename so I can insert each gallery blob into the correct database row.

According to the API docs for the media manager, I can get the original filename, and the fileUrl.

The docs show this example of the fileUrl:
“fileUrl”: “wix:image://v1/f6c0f9_tg439f4475a749e181dd14407fdbd37e~mv2.jpg/original-name.jpg#originWidth=300&originHeight=300”,

Which looks ALMOST like the src attribute in the Gallery…I’ve highlighted the differences in red. Can I assume these are the same?

type is easy. they’re all type image.

What I don’t know how to deal with is SLUG. slug is an attribute on Gallery.items (ImageItem) I actually have no idea what slug is, or whether it’s required, or what happens if you don’t have a slug…and the documentation is…less than helpful…

slug: string Item slug.

(What IS a slug?)

Slug does look suspiciously similar to the fileName part of the src field, and possibly parseable from src, but I’m not sure if that’s actually a guarantee.

I have heard others propose a workaround for getting the slug. They suggest creating a gallery of all of your images, then iterating through all of the items pulling the slug and filename out.

But even if I do resolve the slug issue,

I still don’t see any way of getting at the FileInfo. Nothing that I can see gives me the filename. Without the FileInfo, I have no way of mapping a Gallery.ImageItem to a MediaManager.FileInfo to my Collection row ID.

Is the only way to do this REALLY to programmatically upload images, capturing their elusive filename attribute?

Once a file is in the media manager, is it basically LOST to me? I can never touch it programmatically again?

Am I crazy here?

Hi :raised_hand_with_fingers_splayed:

If I’m not mistaken, you have problem getting the URL of the uploaded images, right?

Before you insert the images, you need to upload them, then insert their URL as a value of the image.

You might find this post helpful:
https://www.wix.com/corvid/forum/main/comment/5ef24fd4caff62002dd6f1e7

If you need further assistance, please reply back.

Ahmad

I know you can get the URL/etc when you programmatically upload a file. But how can you get this information if it’s ALREADY uploaded (ie, if I use the Wix UI to bulk upload all of the images at once, instead of having to do it for every single database insert)

It seems like if the image is already in the Media Manager, there is no way to programmatically get any information about it unless you have either the Wix filename, or the upload URL.

The only API that I’ve found to interact with Wix media is wix-media-backend

The mediaManager module contains methods like getFileInfo()

You can pass this EITHER the Wix URL or the Media Manager file name.

If a file is ALREADY uploaded, the only way I have found to get the URL is to manually open the media manager, right click on the image, and choose “copy URL”.

If I have to MANUALLY do this, then there is no point in trying to automate linking the images to the database. Why bother right clicking and copying the URL so I can “automate” the gallery creation when at that point, I have already done the manual work and could have spent less manual effort just manually linking the image to the database collection.

So…my question is.

If a file is already in the Media Manager, how can I programmatically get any information about it?

The only thing I can think of is to create a gallery that contains every single image, then use the Gallery API to iterate through every item in the gallery, copying the URL that is associated with it, then using those URLs to look up the fileInfo using wix-media-backend.getFileInfo.

From there, I can link these three key pieces of information:

  1. The Wix filename
  2. The original filename
  3. The slug

Which I can map to the database ID that this image belongs to. And now I have all the pieces of information I need to programmatically create a gallery, populate it with only the images for THAT database row, and then save it to my database.

This seems like a nightmare.

If I don’t go that route, and I programmatically upload all the images, capturing their FileInfo as I go, then I can create MOST of the gallery blob, BUT I HAVE NO IDEA WHAT THE SLUG IS . There is no slug in an image’s FileInfo that is returned from the upload. Slug is a Gallery.ImageInfo property. It is NOT a property on MediaManager.FileInfo.

But the gallery requires (?) this field. Below is the blob that was automatically create the last time I did this:

{
“slug”:“c565a4_7e985bb77aa54cc2b121e60081056974mv2”,
“src”:“wix:image://v1/c565a4_7e985bb77aa54cc2b121e60081056974~mv2.jpg/0001_sjpg#originWidth=600&originHeight=400”,
“type”:“image”
}

The Wix API docs for manipulating Gallery content has this example:

https://www.wix.com/corvid/reference/$w.Gallery.html#items

let items = $w ( “#myGallery” ). items ;
items . push ( {
“src” : “wix:image://v1/68d3a9_1de7529c444b4c9eb38401f8efe0cad2.jpg/flowers.jpg#originWidth=1970&originHeight=1120” ,
“description” : “Description” ,
“title” : “Title”
} );
$w ( “#myGallery” ). items = items ;

Which doesn’t show that you have to set the slug.

Does anyone know what the slug is? Why it’s set? Why it’s necessary? And if you’re programmatically trying to manipulate the contents of a gallery, WHAT DO YOU SET FOR SLUG?

Polkaset, did you get a solution for retrieving URL info for files already in Media Manager in order to insert Wix link for image in datasource collection?
Thanks.

@dsac No. This still does not exist. I had to put every image into a gallery, then loop through the gallery capturing all the file info. Of course this is still manual because I have to manually add each new image to a gallery.