Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit 9f950b5

Browse files
authored
fix: global media_library not propagating to fields (#1005)
1 parent f41bb24 commit 9f950b5

File tree

2 files changed

+119
-8
lines changed

2 files changed

+119
-8
lines changed

packages/core/src/actions/__tests__/config.spec.ts

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,112 @@ describe('config', () => {
206206
});
207207
});
208208

209+
describe('media_library', () => {
210+
it('should set media_library based on global config if not set', () => {
211+
const config = createMockConfig({
212+
media_library: {
213+
max_file_size: 600000,
214+
folder_support: true,
215+
},
216+
collections: [
217+
{
218+
name: 'foo-collection',
219+
label: 'Foo',
220+
folder: 'foo',
221+
fields: [{ name: 'title', widget: 'file' }],
222+
},
223+
],
224+
});
225+
226+
const collection = config.collections[0] as FolderCollection;
227+
expect(collection.media_library).toEqual({
228+
max_file_size: 600000,
229+
folder_support: true,
230+
});
231+
232+
const field = collection.fields[0] as FileOrImageField;
233+
expect(field.media_library).toEqual({
234+
max_file_size: 600000,
235+
folder_support: true,
236+
});
237+
});
238+
239+
it('should set media_library based on collection config if set', () => {
240+
const config = createMockConfig({
241+
media_library: {
242+
max_file_size: 600000,
243+
folder_support: true,
244+
},
245+
collections: [
246+
{
247+
name: 'foo-collection',
248+
label: 'Foo',
249+
folder: 'foo',
250+
media_library: {
251+
max_file_size: 500,
252+
folder_support: false,
253+
},
254+
fields: [{ name: 'title', widget: 'file' }],
255+
},
256+
],
257+
});
258+
259+
const collection = config.collections[0] as FolderCollection;
260+
expect(collection.media_library).toEqual({
261+
max_file_size: 500,
262+
folder_support: false,
263+
});
264+
265+
const field = collection.fields[0] as FileOrImageField;
266+
expect(field.media_library).toEqual({
267+
max_file_size: 500,
268+
folder_support: false,
269+
});
270+
});
271+
272+
it('should not override media_library if set', () => {
273+
const config = createMockConfig({
274+
media_library: {
275+
max_file_size: 600000,
276+
folder_support: true,
277+
},
278+
collections: [
279+
{
280+
name: 'foo-collection',
281+
label: 'Foo',
282+
folder: 'foo',
283+
media_library: {
284+
max_file_size: 500,
285+
folder_support: false,
286+
},
287+
fields: [
288+
{
289+
name: 'title',
290+
widget: 'file',
291+
media_library: {
292+
max_file_size: 1000,
293+
folder_support: true,
294+
},
295+
},
296+
],
297+
},
298+
],
299+
});
300+
301+
const collection = config.collections[0] as FolderCollection;
302+
expect(collection.media_library).toEqual({
303+
max_file_size: 500,
304+
folder_support: false,
305+
});
306+
307+
const field = collection.fields[0] as FileOrImageField;
308+
expect(field.media_library).toEqual({
309+
max_file_size: 1000,
310+
folder_support: true,
311+
});
312+
});
313+
});
314+
209315
describe('public_folder', () => {
210316
it('should set public_folder based on media_folder if not set', () => {
211317
expect(
@@ -344,7 +450,7 @@ describe('config', () => {
344450
).toEqual('');
345451
});
346452

347-
it("should set collection media_folder and public_folder to an empty string when collection path exists, but collection media_folder doesn't", () => {
453+
it('should set collection media_folder and public_folder to an empty string when collection path exists, but collection media_folder does not', () => {
348454
const result = createMockConfig({
349455
collections: [
350456
{

packages/core/src/actions/config.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ function getConfigUrl() {
6464
}
6565

6666
const setFieldDefaults =
67-
(collection: Collection, collectionFile?: CollectionFile) => (field: Field) => {
67+
(collection: Collection, config: Config, collectionFile?: CollectionFile) => (field: Field) => {
6868
if ('media_folder' in field && !('public_folder' in field)) {
6969
return { ...field, public_folder: field.media_folder };
7070
}
7171

7272
if (field.widget === 'image' || field.widget === 'file' || field.widget === 'markdown') {
7373
field.media_library = {
74-
...((collectionFile ?? collection).media_library ?? {}),
74+
...(config.media_library ?? {}),
75+
...(collectionFile?.media_library ?? {}),
76+
...(collection.media_library ?? {}),
7577
...(field.media_library ?? {}),
7678
};
7779
}
@@ -139,6 +141,7 @@ function throwOnMissingDefaultLocale(i18n?: Partial<I18nInfo>) {
139141
function applyFolderCollectionDefaults(
140142
originalCollection: FolderCollection,
141143
collectionI18n: I18nInfo | undefined,
144+
config: Config,
142145
): FolderCollectionWithDefaults {
143146
const collection: FolderCollectionWithDefaults = {
144147
...originalCollection,
@@ -155,7 +158,7 @@ function applyFolderCollectionDefaults(
155158
}
156159

157160
if ('fields' in collection && collection.fields) {
158-
collection.fields = traverseFields(collection.fields, setFieldDefaults(collection));
161+
collection.fields = traverseFields(collection.fields, setFieldDefaults(collection, config));
159162
}
160163

161164
collection.folder = trim(collection.folder, '/');
@@ -168,6 +171,7 @@ function applyCollectionFileDefaults(
168171
originalFile: CollectionFile,
169172
collection: Collection,
170173
collectionI18n: I18nInfo | undefined,
174+
config: Config,
171175
): CollectionFileWithDefaults {
172176
const file: CollectionFileWithDefaults = {
173177
...originalFile,
@@ -186,7 +190,7 @@ function applyCollectionFileDefaults(
186190
};
187191

188192
if (file.fields) {
189-
file.fields = traverseFields(file.fields, setFieldDefaults(collection, file));
193+
file.fields = traverseFields(file.fields, setFieldDefaults(collection, config, file));
190194
}
191195

192196
let fileI18n: I18nInfo | undefined;
@@ -219,12 +223,13 @@ function applyCollectionFileDefaults(
219223
function applyFilesCollectionDefaults(
220224
originalCollection: FilesCollection,
221225
collectionI18n: I18nInfo | undefined,
226+
config: Config,
222227
): FilesCollectionWithDefaults {
223228
const collection: FilesCollectionWithDefaults = {
224229
...originalCollection,
225230
i18n: collectionI18n,
226231
files: originalCollection.files.map(f =>
227-
applyCollectionFileDefaults(f, originalCollection, collectionI18n),
232+
applyCollectionFileDefaults(f, originalCollection, collectionI18n, config),
228233
),
229234
};
230235

@@ -248,9 +253,9 @@ function applyCollectionDefaults(
248253
}
249254

250255
if ('folder' in originalCollection) {
251-
collection = applyFolderCollectionDefaults(originalCollection, collectionI18n);
256+
collection = applyFolderCollectionDefaults(originalCollection, collectionI18n, config);
252257
} else {
253-
collection = applyFilesCollectionDefaults(originalCollection, collectionI18n);
258+
collection = applyFilesCollectionDefaults(originalCollection, collectionI18n, config);
254259
}
255260

256261
if (config.editor && !collection.editor) {

0 commit comments

Comments
 (0)