28
Support for media folders in the content API
D
Derrickmehaffy
With the upcoming release of Strapi v4.3.0 and Media folders, the content API will not be able to utilize them. For those that believe it would be a good addition to also have folders at the content API level please leave a vote and comment what your use case would be.
Activity Feed
Sort by
Lucas S.
Having the ability to dynamically create folders on upload would make migrations so much easier. Imagine a structure with hundreds of folders organized by a client and we have to keep that same structure on the admin UI. Automation would be a breeze, it is a quality of life feature.
Tobias
Here a solution that uses the upload plugin's own methods:
// Generate folders
async createFolders(order) {
// Create year media folder if not exists
const year = new Date().getFullYear().toString()
let yearFolder = await strapi.query('plugin::upload.folder').findOne({where: {name: year}});
if (!yearFolder) {
await strapi.plugins.upload.services.folder.create({name: year})
yearFolder = await strapi.query('plugin::upload.folder').findOne({where: {name: year}});
}
// Create order media folder
await strapi.plugins.upload.services.folder.create({name: order, parent: yearFolder.id})
return await strapi.query('plugin::upload.folder').findOne({where: {name: order, parent: yearFolder}});
},
// Move files into folder
async moveOrderFilesToFolder(order, folder) {
let update = []
const files = (await strapi.db.query("api::order.order").findOne({
where: { id: order.id },
populate: {
files: true,
}
})).files
if(files !== null) {
for (const file of files) {
update.push(await strapi.plugins.upload.services.upload.updateFileInfo(file.id, {
name: file.name,
alternativeText: file.alternativeText,
caption: order.attributes.order,
folder: folder.id,
folderPath: folder.path
}))
}
return update
}
},
A
Alagunasalahaddin
In our case, we are trying to automatize our folders so all our clients can have a lead of how they must to work with the media library.
All of this is from the backend.
Tobias
push
Tobias
gu-stav any update on this?
Would be sufficient to be able to additionaly define the folder in formData like this (no need to create the folder on the file system, it should just set the attribute in the database like the admin panel does):
<form>
<input type="text" name="folder" /> <!-- NEW -->
<input type="file" name="files" />
<input type="text" name="ref" value="api::restaurant.restaurant" />
<input type="text" name="refId" value="5c126648c7415f0c0ef1bccd" />
<input type="text" name="field" value="cover" />
<input type="submit" value="Submit" />
</form>
Rolf Veinø Sørensen
I would expect the public api to have the same abilities for the media library as the strapi admin interface does. This allows developers of websites to create a full features media browser/library with folders.
B
Bolk
In addition to this feature request, it would be helpful as well to add metadata to file uploads.
D
DT
I would suggest to have the ability to tag the folder (for team access) or to name the folder by username. Or at least fill "created_by_id" field.
That would let end users to see only their own uploads and manage it without seeing whole bunch of uploads.
Also total size count per tag/username would greatly help to prevent upload API abuse.
Also "update" endpoint could be useful
Tobias
It would be creat if the REST API upload plugin would accept a path parameter to determine the folder. In my use case all media ist created via formData by users.
Use-case:
I use strapi as backend for a architectural house model ordering service. The nuxt frontend allows our customers to use a threejs house configurator and upload floor plans with their order. The order and all associated pdfs, drawings, created qr codes etc are uploaded then on strapi via the nuxtjs/strapi plugin that uses the rest api:
const formData = new FormData()
// not uploaded but created by js
formData.append('files.qrcode', blob, this.order.order + '.svg')
// uploaded via input field
this.order.files.forEach((file) => {
formData.append(
files.files
, file)})
Now, the documents of a order/user are linked in the order relation. But unfortunately all documents of all orders/users are stored in the media library in the same folder which makes it almost impossible to find documents again in the library.
I would love if I could pass a path in the filename both for self created files and files via form input. The files must not necessarily be located in the respected path in the file system but it would be great if the path would determine 4.3's media library folders in the admin gui (i.e. path only stored in internal db).
This should be just a small code change for the REST API but would open up a lot new opportunities for us.