Skip to content

Commit

Permalink
fix the issue of the red cross icon appearing for PDF images in the d…
Browse files Browse the repository at this point in the history
…ataset table
  • Loading branch information
Junjiequan committed Nov 20, 2024
1 parent deff61a commit 24dd510
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/app/datasets/dataset-detail/dataset-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ENTER, COMMA, SPACE } from "@angular/cdk/keycodes";
import { MatChipInputEvent } from "@angular/material/chips";

import { MatDialog } from "@angular/material/dialog";
import { SampleEditComponent } from "datasets/sample-edit/sample-edit.component";
// import { SampleEditComponent } from "datasets/sample-edit/sample-edit.component";
import { DialogComponent } from "shared/modules/dialog/dialog.component";
import { combineLatest, fromEvent, Observable, Subscription } from "rxjs";
import { Store } from "@ngrx/store";
Expand Down Expand Up @@ -354,11 +354,7 @@ export class DatasetDetailComponent
}

getImageUrl(encoded: string) {
const mimeType = this.base64MimeType(encoded);
if (mimeType === "application/pdf") {
return "assets/images/pdf-icon.svg";
}
return encoded;
return this.attachmentService.getImageUrl(encoded);
}

openAttachment(encoded: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ export class FileUploaderComponent {
}

getImageUrl(encoded: string) {
const mimeType = this.base64MimeType(encoded);
if (mimeType === "application/pdf") {
return "assets/images/pdf-icon.svg";
}
return encoded;
return this.attachmentService.getImageUrl(encoded);
}

openAttachment(encoded: string) {
Expand Down
15 changes: 15 additions & 0 deletions src/app/shared/services/attachment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Injectable } from "@angular/core";
@Injectable()
export class AttachmentService {
base64MimeType(encoded: string): string {
if (!encoded) return null;

let result = null;

if (typeof encoded !== "string") {
Expand All @@ -17,7 +19,20 @@ export class AttachmentService {

return result;
}

getImageUrl(encoded: string) {
if (!encoded) return null;

const mimeType = this.base64MimeType(encoded);
if (mimeType === "application/pdf") {
return "assets/images/pdf-icon.svg";
}
return encoded;
}

openAttachment(encoded: string) {
if (!encoded) return null;

const mimeType = this.base64MimeType(encoded);
const strippedData = encoded.replace(
new RegExp(`^data:${mimeType};base64,`),
Expand Down
4 changes: 3 additions & 1 deletion src/app/shared/services/thumbnail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { distinctUntilChanged, firstValueFrom } from "rxjs";
import { DatasetApi } from "shared/sdk";
import { selectDatasetsPerPage } from "state-management/selectors/datasets.selectors";
import { AppConfigService } from "app-config.service";
import { AttachmentService } from "./attachment.service";

interface ThumbnailCache {
[pid: string]: {
Expand All @@ -25,6 +26,7 @@ export class ThumbnailService {
constructor(
private datasetApi: DatasetApi,
private store: Store,
private attachmentService: AttachmentService,
private appConfigService: AppConfigService,
) {
this.store
Expand Down Expand Up @@ -60,7 +62,7 @@ export class ThumbnailService {
try {
const encodedPid = encodeURIComponent(pid);
const res = await firstValueFrom(this.datasetApi.thumbnail(encodedPid));
const thumbnail = res?.thumbnail || null;
const thumbnail = this.attachmentService.getImageUrl(res?.thumbnail);

this.thumbnailCache[pid] = {
value: thumbnail,
Expand Down

0 comments on commit 24dd510

Please sign in to comment.