Skip to content

Commit

Permalink
chore(edit-content): Fix error with selecting files #30060
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Nov 20, 2024
1 parent 07c4b5a commit 338fa2b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
@let previewFile = $previewFile();
@let metadata = $metadata();
@let downloadLink = $downloadLink();
@let fileInfo = $fileInfo();
@let metadata = fileInfo.metadata;

<div class="preview-container" [class.preview-container--fade]="metadata?.editableAsText">
@if (metadata?.editableAsText) {
<div class="preview-code_container" data-testId="code-preview">
<code>{{ $content() }}</code>
<code>{{ fileInfo.content }}</code>
</div>
} @else {
<div class="preview-image__container">
@if (previewFile.source === 'temp') {
@if (fileInfo.source === 'temp') {
<dot-temp-file-thumbnail
[tempFile]="previewFile.file"
[tempFile]="fileInfo.file"
[iconSize]="'48px'"
data-testId="temp-file-thumbnail" />
} @else {
<dot-contentlet-thumbnail
[fieldVariable]="$fieldVariable()"
[fieldVariable]="fileInfo.contentType"
[iconSize]="'48px'"
[contentlet]="previewFile.file"
[contentlet]="fileInfo.file"
[playableVideo]="true"
data-testId="contentlet-thumbnail" />
}
Expand Down Expand Up @@ -56,12 +55,12 @@
data-testId="info-btn"
icon="pi pi-info" />

@if (downloadLink) {
@if (fileInfo.downloadLink) {
<p-button
styleClass="p-button-rounded p-button-sm p-button-outlined"
data-testId="download-btn"
icon="pi pi-download"
(click)="downloadAsset(downloadLink)" />
(click)="downloadAsset(fileInfo.downloadLink)" />
}
</div>
<div class="preview-metadata__actions">
Expand All @@ -79,12 +78,12 @@
styleClass="p-button-rounded"
data-testId="info-btn-responsive"
icon="pi pi-info" />
@if (downloadLink) {
@if (fileInfo.downloadLink) {
<p-button
styleClass="p-button-rounded"
data-testId="download-btn-responsive"
icon="pi pi-download"
(click)="downloadAsset(downloadLink)" />
(click)="downloadAsset(fileInfo.downloadLink)" />
}
<p-button
(click)="removeFile.emit()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { DialogModule } from 'primeng/dialog';
import { catchError } from 'rxjs/operators';

import { DotResourceLinksService } from '@dotcms/data-access';
import { DotCMSBaseTypesContentTypes, DotCMSContentlet } from '@dotcms/dotcms-models';
import { DotCMSBaseTypesContentTypes, DotCMSContentlet, DotFileMetadata } from '@dotcms/dotcms-models';
import {
DotPreviewResourceLink,
UploadedFile
Expand All @@ -30,8 +30,16 @@ import {
DotCopyButtonComponent
} from '@dotcms/ui';

import { CONTENT_TYPES, DEFAULT_CONTENT_TYPE } from '../../dot-edit-content-file-field.const';
import { getFileMetadata } from '../../utils';

type FileInfo = UploadedFile & {
contentType: string;
downloadLink: string;
content: string | null;
metadata: DotFileMetadata;
};

@Component({
selector: 'dot-file-field-preview',
standalone: true,
Expand All @@ -57,13 +65,6 @@ export class DotFileFieldPreviewComponent implements OnInit {
* @memberof DotFileFieldPreviewComponent
*/
$previewFile = input.required<UploadedFile>({ alias: 'previewFile' });

/**
* Field variable
*
* @memberof DotFileFieldPreviewComponent
*/
$fieldVariable = input.required<string>({ alias: 'fieldVariable' });
/**
* Remove file
*
Expand All @@ -77,47 +78,36 @@ export class DotFileFieldPreviewComponent implements OnInit {
*/
$showDialog = signal(false);
/**
* File metadata
*
* @memberof DotFileFieldPreviewComponent
*/
$metadata = computed(() => {
const previewFile = this.$previewFile();
if (previewFile.source === 'temp') {
return previewFile.file.metadata;
}

return getFileMetadata(previewFile.file);
});
/**
* Content
* File info
*
* @memberof DotFileFieldPreviewComponent
*/
$content = computed(() => {
$fileInfo = computed<FileInfo>(() => {
const previewFile = this.$previewFile();
if (previewFile.source === 'contentlet') {
return previewFile.file.content;
}

return null;
});
/**
* Download link
*
* @memberof DotFileFieldPreviewComponent
*/
$downloadLink = computed(() => {
const previewFile = this.$previewFile();
const fieldVariable = this.$fieldVariable();

if (previewFile.source === 'contentlet') {
const file = previewFile.file;

return `/contentAsset/raw-data/${file.inode}/${fieldVariable}?byInode=true&force_download=true`;
const contentType = CONTENT_TYPES[file.contentType] || DEFAULT_CONTENT_TYPE;

return {
source: previewFile.source,
file,
content: file.content,
contentType,
downloadLink: `/contentAsset/raw-data/${file.inode}/${contentType}?byInode=true&force_download=true`,
metadata: getFileMetadata(file)
};
}

return null;
return {
source: previewFile.source,
file: previewFile.file,
content: null,
contentType: DEFAULT_CONTENT_TYPE,
downloadLink: null,
metadata: previewFile.file.metadata
};
});

/**
Expand All @@ -135,10 +125,10 @@ export class DotFileFieldPreviewComponent implements OnInit {
* @memberof DotFileFieldPreviewComponent
*/
ngOnInit() {
const previewFile = this.$previewFile();
const fileInfo = this.$fileInfo();

if (previewFile.source === 'contentlet') {
this.fetchResourceLinks(previewFile.file);
if (fileInfo.source === 'contentlet') {
this.fetchResourceLinks(fileInfo.file, fileInfo.contentType);
}
}

Expand Down Expand Up @@ -169,10 +159,10 @@ export class DotFileFieldPreviewComponent implements OnInit {
* @param {DotCMSContentlet} contentlet The contentlet to fetch the resource links for.
* @memberof DotFileFieldPreviewComponent
*/
private fetchResourceLinks(contentlet: DotCMSContentlet): void {
private fetchResourceLinks(contentlet: DotCMSContentlet, contentType: string): void {
this.#dotResourceLinksService
.getFileResourceLinks({
fieldVariable: this.$fieldVariable(),
fieldVariable: contentType,
inodeOrIdentifier: contentlet.identifier
})
.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
@let uploadedFile = store.uploadedFile();
@if (uploadedFile) {
<dot-file-field-preview
[fieldVariable]="'asset'"
(removeFile)="store.removeFile()"
[previewFile]="uploadedFile" />
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ export const INPUT_CONFIG: ConfigActions = {
maxFileSize: null
}
};

export const DEFAULT_CONTENT_TYPE = 'asset';


export const CONTENT_TYPES = {
'DotAsset': 'asset',
'FileAsset': 'fileAsset',
};
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ export class DotEditContentService {
hostFolderId: folderId,
showLinks: false,
showDotAssets: true,
showPages: true,
showPages: false,
showFiles: true,
showFolders: false,
showWorking: true,
showArchived: true,
showArchived: false,
sortByDesc: true,
mimeTypes
};
Expand Down

0 comments on commit 338fa2b

Please sign in to comment.