Skip to content

Commit

Permalink
feat(IntegrationMultipleFileDownload): do not zip if only 1 file is s…
Browse files Browse the repository at this point in the history
…elected
  • Loading branch information
MellyGray committed Dec 14, 2023
1 parent c9cb3fe commit 66f2331
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 45 deletions.
8 changes: 1 addition & 7 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,11 @@ export class FileSize {
}
}

export enum FileDownloadSizeMode {
ALL = 'All',
ORIGINAL = 'Original',
ARCHIVAL = 'Archival'
}

export class FileDownloadSize extends FileSize {
constructor(
readonly value: number,
readonly unit: FileSizeUnit,
readonly mode: FileDownloadSizeMode
readonly mode: FileDownloadMode
) {
super(value, unit)
}
Expand Down
5 changes: 3 additions & 2 deletions src/files/domain/repositories/FileRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { File } from '../models/File'
import { File, FileDownloadMode } from '../models/File'
import { FileCriteria } from '../models/FileCriteria'
import { FilesCountInfo } from '../models/FilesCountInfo'
import { FilePaginationInfo } from '../models/FilePaginationInfo'
Expand All @@ -23,5 +23,6 @@ export interface FileRepository {
criteria?: FileCriteria
) => Promise<number>
getUserPermissionsById: (id: number) => Promise<FileUserPermissions>
getMultipleFileDownloadUrl: (ids: number[], downloadMode: string) => string
getMultipleFileDownloadUrl: (ids: number[], downloadMode: FileDownloadMode) => string
getFileDownloadUrl: (id: number, downloadMode: FileDownloadMode) => string
}
5 changes: 5 additions & 0 deletions src/files/domain/useCases/getMultipleFileDownloadUrl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { FileRepository } from '../repositories/FileRepository'
import { FileDownloadMode } from '../models/File'

const ONLY_ONE_FILE = 1
export function getMultipleFileDownloadUrl(
fileRepository: FileRepository,
ids: number[],
downloadMode: FileDownloadMode
): string {
if (ids.length === ONLY_ONE_FILE) {
return fileRepository.getFileDownloadUrl(ids[0], downloadMode)
}

return fileRepository.getMultipleFileDownloadUrl(ids, downloadMode)
}
19 changes: 13 additions & 6 deletions src/files/infrastructure/FileJSDataverseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { FileRepository } from '../domain/repositories/FileRepository'
import { File } from '../domain/models/File'
import { File, FileDownloadMode } from '../domain/models/File'
import { FilesCountInfo } from '../domain/models/FilesCountInfo'
import { FilePaginationInfo } from '../domain/models/FilePaginationInfo'
import { FileUserPermissions } from '../domain/models/FileUserPermissions'
import {
File as JSFile,
FileDataTable as JSFileTabularData,
FileDownloadSizeMode,
getDatasetFileCounts,
getDatasetFiles,
getDatasetFilesTotalDownloadSize,
getFileDataTables,
getFileDownloadCount,
getFileUserPermissions,
ReadError,
File as JSFile,
getFileDataTables,
FileDataTable as JSFileTabularData
ReadError
} from '@iqss/dataverse-client-javascript'
import { FileCriteria } from '../domain/models/FileCriteria'
import { DomainFileMapper } from './mappers/DomainFileMapper'
Expand Down Expand Up @@ -156,7 +156,14 @@ export class FileJSDataverseRepository implements FileRepository {
})
}

getMultipleFileDownloadUrl(ids: number[], downloadMode: string): string {
getMultipleFileDownloadUrl(ids: number[], downloadMode: FileDownloadMode): string {
return `/api/access/datafiles/${ids.join(',')}?format=${downloadMode}`
}

getFileDownloadUrl(id: number, downloadMode: FileDownloadMode): string {
if (downloadMode === FileDownloadMode.ORIGINAL) {
return `/api/access/datafile/${id}?format=${downloadMode}`
}
return `/api/access/datafile/${id}`
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { DropdownButton, DropdownButtonItem, DropdownHeader } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'

import { FileDownloadSize, FileDownloadSizeMode } from '../../../../files/domain/models/File'
import { FileDownloadMode, FileDownloadSize } from '../../../../files/domain/models/File'
import { Download } from 'react-bootstrap-icons'

interface AccessDatasetMenuProps {
Expand All @@ -30,12 +30,12 @@ export function AccessDatasetMenu({
return <></>
}

function getFormattedFileSize(mode: FileDownloadSizeMode): string {
function getFormattedFileSize(mode: FileDownloadMode): string {
const foundSize = fileDownloadSizes && fileDownloadSizes.find((size) => size.mode === mode)
return foundSize ? foundSize.toString() : ''
}

const handleDownload = (type: FileDownloadSizeMode) => {
const handleDownload = (type: FileDownloadMode) => {
//TODO: implement download feature
console.log('downloading file ' + type)
}
Expand All @@ -47,19 +47,19 @@ export function AccessDatasetMenu({
const DatasetDownloadOptions = ({ datasetContainsTabularFiles }: DatasetDownloadOptionsProps) => {
return datasetContainsTabularFiles ? (
<>
<DropdownButtonItem onClick={() => handleDownload(FileDownloadSizeMode.ORIGINAL)}>
<DropdownButtonItem onClick={() => handleDownload(FileDownloadMode.ORIGINAL)}>
{t('datasetActionButtons.accessDataset.downloadOriginalZip')} (
{getFormattedFileSize(FileDownloadSizeMode.ORIGINAL)})
{getFormattedFileSize(FileDownloadMode.ORIGINAL)})
</DropdownButtonItem>
<DropdownButtonItem onClick={() => handleDownload(FileDownloadSizeMode.ARCHIVAL)}>
<DropdownButtonItem onClick={() => handleDownload(FileDownloadMode.ARCHIVAL)}>
{t('datasetActionButtons.accessDataset.downloadArchiveZip')} (
{getFormattedFileSize(FileDownloadSizeMode.ARCHIVAL)})
{getFormattedFileSize(FileDownloadMode.ARCHIVAL)})
</DropdownButtonItem>
</>
) : (
<DropdownButtonItem onClick={() => handleDownload(FileDownloadSizeMode.ORIGINAL)}>
<DropdownButtonItem onClick={() => handleDownload(FileDownloadMode.ORIGINAL)}>
{t('datasetActionButtons.accessDataset.downloadZip')} (
{getFormattedFileSize(FileDownloadSizeMode.ORIGINAL)})
{getFormattedFileSize(FileDownloadMode.ORIGINAL)})
</DropdownButtonItem>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export function DownloadFilesButton({ files, fileSelection }: DownloadFilesButto
const { dataset } = useDataset()
const [showNoFilesSelectedModal, setShowNoFilesSelectedModal] = useState(false)
const { getMultipleFileDownloadUrl } = useMultipleFileDownload()
const fileSelectionCount = Object.keys(fileSelection).length
const onClick = (event: MouseEvent<HTMLButtonElement>) => {
if (Object.keys(fileSelection).length === SELECTED_FILES_EMPTY) {
if (fileSelectionCount === SELECTED_FILES_EMPTY) {
event.preventDefault()
setShowNoFilesSelectedModal(true)
}
Expand Down
9 changes: 7 additions & 2 deletions src/stories/files/FileMockRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FileRepository } from '../../files/domain/repositories/FileRepository'
import { FilesMockData } from './FileMockData'
import { File } from '../../files/domain/models/File'
import { File, FileDownloadMode } from '../../files/domain/models/File'
import { FilesCountInfo } from '../../files/domain/models/FilesCountInfo'
import { FilesCountInfoMother } from '../../../tests/component/files/domain/models/FilesCountInfoMother'
import { FilePaginationInfo } from '../../files/domain/models/FilePaginationInfo'
Expand Down Expand Up @@ -62,7 +62,12 @@ export class FileMockRepository implements FileRepository {
}

// eslint-disable-next-line unused-imports/no-unused-vars
getMultipleFileDownloadUrl(ids: number[], downloadMode: string): string {
getMultipleFileDownloadUrl(ids: number[], downloadMode: FileDownloadMode): string {
return FileMother.createDownloadUrl()
}

// eslint-disable-next-line unused-imports/no-unused-vars
getFileDownloadUrl(id: number, downloadMode: FileDownloadMode): string {
return FileMother.createDownloadUrl()
}
}
12 changes: 6 additions & 6 deletions tests/component/dataset/domain/models/DatasetMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
MetadataBlockName
} from '../../../../../src/dataset/domain/models/Dataset'
import {
FileDownloadMode,
FileDownloadSize,
FileDownloadSizeMode,
FileSizeUnit
} from '../../../../../src/files/domain/models/File'

Expand Down Expand Up @@ -199,16 +199,16 @@ export class DatasetFileDownloadSizeMother {
return new FileDownloadSize(
props?.value ?? faker.datatype.number(),
props?.unit ?? faker.helpers.arrayElement(Object.values(FileSizeUnit)),
props?.mode ?? faker.helpers.arrayElement(Object.values(FileDownloadSizeMode))
props?.mode ?? faker.helpers.arrayElement(Object.values(FileDownloadMode))
)
}

static createArchival(): FileDownloadSize {
return this.create({ mode: FileDownloadSizeMode.ARCHIVAL })
return this.create({ mode: FileDownloadMode.ARCHIVAL })
}

static createOriginal(): FileDownloadSize {
return this.create({ mode: FileDownloadSizeMode.ORIGINAL })
return this.create({ mode: FileDownloadMode.ORIGINAL })
}
}

Expand Down Expand Up @@ -482,8 +482,8 @@ export class DatasetMother {
hasValidTermsOfAccess: true,
hasOneTabularFileAtLeast: true,
fileDownloadSizes: [
new FileDownloadSize(21.98, FileSizeUnit.KILOBYTES, FileDownloadSizeMode.ORIGINAL),
new FileDownloadSize(21.98, FileSizeUnit.KILOBYTES, FileDownloadSizeMode.ARCHIVAL)
new FileDownloadSize(21.98, FileSizeUnit.KILOBYTES, FileDownloadMode.ORIGINAL),
new FileDownloadSize(21.98, FileSizeUnit.KILOBYTES, FileDownloadMode.ARCHIVAL)
],
isValid: true,
...props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ describe('AccessDatasetMenu', () => {
cy.findByRole('button', { name: 'Access Dataset' }).should('exist')
cy.findByRole('button', { name: 'Access Dataset' }).click()
cy.contains('Original Format ZIP').click()
cy.get('@consoleLog').should('have.been.calledWith', 'downloading file Original')
cy.get('@consoleLog').should('have.been.calledWith', 'downloading file original')
cy.findByRole('button', { name: 'Access Dataset' }).click()
cy.contains('Archive Format').click()
cy.get('@consoleLog').should('have.been.calledWith', 'downloading file Archival')
cy.get('@consoleLog').should('have.been.calledWith', 'downloading file archival')
})

it('renders the AccessDatasetMenu if the user has download files permissions and the dataset is not deaccessioned', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('DownloadFilesButton', () => {
)
}

beforeEach(() => {
fileRepository.getMultipleFileDownloadUrl = cy
.stub()
.returns('https://multiple-file-download-url')
})

it('renders the Download Files button if there is more than 1 file in the dataset and the user has download files permission', () => {
const datasetWithDownloadFilesPermission = DatasetMother.create({
permissions: DatasetPermissionsMother.createWithFilesDownloadAllowed()
Expand Down Expand Up @@ -141,7 +147,6 @@ describe('DownloadFilesButton', () => {
'some-file-id': files[0],
'some-other-file-id': files[1]
}
fileRepository.getMultipleFileDownloadUrl = cy.stub().returns('https://some-download-url')
cy.mountAuthenticated(
<MultipleFileDownloadProvider repository={fileRepository}>
{withDataset(
Expand All @@ -153,7 +158,7 @@ describe('DownloadFilesButton', () => {

cy.findByRole('button', { name: 'Download' })
.parent('a')
.should('have.attr', 'href', 'https://some-download-url')
.should('have.attr', 'href', 'https://multiple-file-download-url')
})

it('renders the download url for the selected files when some files are selected and there are tabular files', () => {
Expand All @@ -172,7 +177,6 @@ describe('DownloadFilesButton', () => {
'some-file-id': files[0],
'some-other-file-id': files[1]
}
fileRepository.getMultipleFileDownloadUrl = cy.stub().returns('https://some-download-url')
cy.mountAuthenticated(
<MultipleFileDownloadProvider repository={fileRepository}>
{withDataset(
Expand All @@ -186,12 +190,12 @@ describe('DownloadFilesButton', () => {
cy.findByRole('link', { name: 'Original Format' }).should(
'have.attr',
'href',
'https://some-download-url'
'https://multiple-file-download-url'
)
cy.findByRole('link', { name: 'Archival Format (.tab)' }).should(
'have.attr',
'href',
'https://some-download-url'
'https://multiple-file-download-url'
)
})

Expand All @@ -200,8 +204,8 @@ describe('DownloadFilesButton', () => {
permissions: DatasetPermissionsMother.createWithFilesDownloadAllowed(),
hasOneTabularFileAtLeast: true,
downloadUrls: {
original: 'https://some-download-url',
archival: 'https://some-download-url'
original: 'https://dataset-download-url-original',
archival: 'https://dataset-download-url-archival'
}
})
const files = FileMother.createMany(2, {
Expand All @@ -226,12 +230,50 @@ describe('DownloadFilesButton', () => {
cy.findByRole('link', { name: 'Original Format' }).should(
'have.attr',
'href',
'https://some-download-url'
'https://dataset-download-url-original'
)
cy.findByRole('link', { name: 'Archival Format (.tab)' }).should(
'have.attr',
'href',
'https://dataset-download-url-archival'
)
})

it('renders the dataset download url with the single file download url when one file is selected', () => {
const datasetWithDownloadFilesPermission = DatasetMother.create({
permissions: DatasetPermissionsMother.createWithFilesDownloadAllowed(),
hasOneTabularFileAtLeast: true
})
const files = FileMother.createMany(2, {
tabularData: {
variablesCount: 2,
observationsCount: 3,
unf: 'some-unf'
}
})
const fileSelection = {
'some-file-id': files[0]
}
fileRepository.getFileDownloadUrl = cy.stub().returns('https://single-file-download-url')
cy.mountAuthenticated(
<MultipleFileDownloadProvider repository={fileRepository}>
{withDataset(
<DownloadFilesButton files={files} fileSelection={fileSelection} />,
datasetWithDownloadFilesPermission
)}
</MultipleFileDownloadProvider>
)

cy.findByRole('button', { name: 'Download' }).click()
cy.findByRole('link', { name: 'Original Format' }).should(
'have.attr',
'href',
'https://single-file-download-url'
)
cy.findByRole('link', { name: 'Archival Format (.tab)' }).should(
'have.attr',
'href',
'https://some-download-url'
'https://single-file-download-url'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ describe('Dataset', () => {
})
})

it.only('applies filters to the Files Table in the correct order', () => {
it('applies filters to the Files Table in the correct order', () => {
const files = [
FileHelper.create('csv', {
description: 'Some description',
Expand Down

0 comments on commit 66f2331

Please sign in to comment.