I need a way to:
-
Upload a bunch of images (thousands)
-
Programmatically connect those images to a Media Gallery field/column in a Collection
-
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?