Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored download/upload logic to support font faces with multiple src assets #58216

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import CollectionFontDetails from './collection-font-details';
import { toggleFont } from './utils/toggleFont';
import { getFontsOutline } from './utils/fonts-outline';
import GoogleFontsConfirmDialog from './google-fonts-confirm-dialog';
import { downloadFontFaceAsset } from './utils';
import { downloadFontFaceAssets } from './utils';

const DEFAULT_CATEGORY = {
slug: 'all',
Expand Down Expand Up @@ -161,7 +161,7 @@ function FontCollection( { slug } ) {
await Promise.all(
fontFamily.fontFace.map( async ( fontFace ) => {
if ( fontFace.src ) {
fontFace.file = await downloadFontFaceAsset(
fontFace.file = await downloadFontFaceAssets(
fontFace.src
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,25 @@ export function makeFontFacesFormData( font ) {
const fontFacesFormData = font.fontFace.map( ( face, faceIndex ) => {
const formData = new FormData();
if ( face.file ) {
// Slugified file name because the it might contain spaces or characters treated differently on the server.
const fileId = `file-${ faceIndex }`;
// Add the files to the formData
formData.append( fileId, face.file, face.file.name );
// remove the file object from the face object the file is referenced in src
const { file, ...faceWithoutFileProperty } = face;
const fontFaceSettings = {
...faceWithoutFileProperty,
src: fileId,
};
formData.append(
'font_face_settings',
JSON.stringify( fontFaceSettings )
);
// file may be a single file or a collection of files.
// make sure it's an array
pbking marked this conversation as resolved.
Show resolved Hide resolved
const files = Array.isArray( face.file )
? face.file
: [ face.file ];
const src = [];

files.forEach( ( file, key ) => {
// Slugified file name because the it might contain spaces or characters treated differently on the server.
const fileId = `file-${ faceIndex }-${ key }`;
// Add the files to the formData
formData.append( fileId, file, file.name );
src.push( fileId );
} );

face.src = src.length === 1 ? src[ 0 ] : src;
delete face.file;

formData.append( 'font_face_settings', JSON.stringify( face ) );
} else {
formData.append( 'font_face_settings', JSON.stringify( face ) );
}
Expand Down Expand Up @@ -225,31 +230,34 @@ export async function batchInstallFontFaces( fontFamilyId, fontFacesData ) {
/*
* Downloads a font face asset from a URL to the client and returns a File object.
*/
export async function downloadFontFaceAsset( url ) {
return fetch( new Request( url ) )
.then( ( response ) => {
if ( ! response.ok ) {
throw new Error(
`Error downloading font face asset from ${ url }. Server responded with status: ${ response.status }`
);
}
return response.blob();
} )
.then( ( blob ) => {
const filename = url.split( '/' ).pop();
const file = new File( [ blob ], filename, {
type: blob.type,
} );
return file;
export async function downloadFontFaceAssets( src ) {
//src could be a single string or a collection of strings.
//here, just make sure it's an array
pbking marked this conversation as resolved.
Show resolved Hide resolved
src = Array.isArray( src ) ? src : [ src ];

const files = await Promise.all(
creativecoder marked this conversation as resolved.
Show resolved Hide resolved
src.map( async ( url ) => {
return fetch( new Request( url ) )
.then( ( response ) => {
if ( ! response.ok ) {
throw new Error(
`Error downloading font face asset from ${ url }. Server responded with status: ${ response.status }`
);
}
return response.blob();
} )
creativecoder marked this conversation as resolved.
Show resolved Hide resolved
.then( ( blob ) => {
const filename = url.split( '/' ).pop();
const file = new File( [ blob ], filename, {
type: blob.type,
} );
return file;
} );
} )
.catch( ( error ) => {
// eslint-disable-next-line no-console
console.error(
`Error downloading font face asset from ${ url }:`,
error
);
throw error;
} );
pbking marked this conversation as resolved.
Show resolved Hide resolved
);

// If we only have one file return it (not the array). Otherwise return all of them in the array.
return files.length === 1 ? files[ 0 ] : files;
}

/*
Expand Down
Loading