Skip to content

Commit

Permalink
Progres on #25959
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelrojas committed Oct 12, 2023
1 parent b655830 commit 7b9d9f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Expand Up @@ -185,3 +185,5 @@ export interface SocialMediaOption {
icon: string;
description: string;
}

export const IMG_NOT_FOUND_KEY = 'not-found';
@@ -1,4 +1,4 @@
import { Observable, forkJoin, from, of } from 'rxjs';
import { Observable, forkJoin, from, of, throwError } from 'rxjs';

import { Injectable } from '@angular/core';

Expand All @@ -20,7 +20,8 @@ import {
ImageMetaData,
OpenGraphOptions,
SEO_TAGS,
SEO_MEDIA_TYPES
SEO_MEDIA_TYPES,
IMG_NOT_FOUND_KEY
} from '../dot-edit-content-html/models/meta-tags-model';

@Injectable()
Expand Down Expand Up @@ -444,16 +445,22 @@ export class DotSeoMetaTagsService {
const imageOg = metaTagsObject['og:image'];

return this.getImageFileSize(imageOg).pipe(
switchMap((imageMetaData) => {
switchMap((imageMetaData: ImageMetaData) => {
const result: SeoRulesResult[] = [];

if (imageOg && imageMetaData.length <= SEO_LIMITS.MAX_IMAGE_BYTES) {
if (
imageMetaData?.url !== IMG_NOT_FOUND_KEY &&
imageMetaData.length <= SEO_LIMITS.MAX_IMAGE_BYTES
) {
result.push(
this.getDoneItem(this.dotMessageService.get('seo.rules.og-image.found'))
);
}

if (this.areAllFalsyOrEmpty([imageOgElements, imageOg])) {
if (
imageMetaData?.url === IMG_NOT_FOUND_KEY ||
this.areAllFalsyOrEmpty([imageOgElements, imageOg])
) {
result.push(
this.getErrorItem(
this.dotMessageService.get('seo.rules.og-image.not.found')
Expand Down Expand Up @@ -760,6 +767,10 @@ export class DotSeoMetaTagsService {
getImageFileSize(imageUrl: string): Observable<DotCMSTempFile | ImageMetaData> {
return from(fetch(imageUrl)).pipe(
mergeMap((response) => {
if (response.status === 404) {
throwError('Image not found');
}

return response.blob();
}),
mergeMap((response) => {
Expand All @@ -769,10 +780,12 @@ export class DotSeoMetaTagsService {
});
}),
catchError((error) => {
console.warn(
'Getting the file size from an external URL failed, so we upload it to the server:',
error
);
if (error.message === 'Failed to fetch') {
return of({
length: 0,
url: IMG_NOT_FOUND_KEY
});
}

return from(this.dotUploadService.uploadFile({ file: imageUrl })).pipe(
mergeMap((uploadedFile) => {
Expand All @@ -786,7 +799,7 @@ export class DotSeoMetaTagsService {

return of({
length: 0,
url: imageUrl
url: IMG_NOT_FOUND_KEY
});
})
);
Expand Down

0 comments on commit 7b9d9f9

Please sign in to comment.