Skip to content

Commit

Permalink
Added download buttons and fixed downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Oct 4, 2023
1 parent bb18eb9 commit af3c35e
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 69 deletions.
10 changes: 8 additions & 2 deletions src/app/item-page/full/full-item-page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import { MetadataFieldDataService } from 'src/app/core/data/metadata-field-data.
import { MetadataSchemaDataService } from 'src/app/core/data/metadata-schema-data.service';
import { MetadataBitstreamDataService } from 'src/app/core/data/metadata-bitstream-data.service';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';
import { ConfigurationDataService} from '../../core/data/configuration-data.service';
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
import { cold } from 'jasmine-marbles';

const mockItem: Item = Object.assign(new Item(), {
bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])),
Expand Down Expand Up @@ -97,6 +98,11 @@ describe('FullItemPageComponent', () => {
}))
});

let halService: HALEndpointService;
halService = jasmine.createSpyObj('halService', {
'getEndpoint': cold('a', { a: 'endpointURL' })
});


translateService = getMockTranslateService();
TestBed.configureTestingModule({
Expand All @@ -118,7 +124,7 @@ describe('FullItemPageComponent', () => {
{ provide: NotificationsService, useValue: {} },
{ provide: MetadataSchemaDataService, useValue: {} },
{ provide: MetadataFieldDataService, useValue: {} },
{ provide: ConfigurationDataService, useValue: configurationDataService },
{ provide: HALEndpointService, useValue: halService },
RegistryService
],

Expand Down
10 changes: 5 additions & 5 deletions src/app/item-page/full/full-item-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AuthService } from '../../core/auth/auth.service';
import { Location } from '@angular/common';
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
import { RegistryService } from 'src/app/core/registry/registry.service';
import {ConfigurationDataService} from '../../core/data/configuration-data.service';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';


/**
Expand Down Expand Up @@ -52,13 +52,13 @@ export class FullItemPageComponent extends ItemPageComponent implements OnInit,
authorizationService: AuthorizationDataService,
protected registryService: RegistryService,
private _location: Location,
protected configurationService: ConfigurationDataService) {
super(route, router, items, authService, authorizationService, registryService, configurationService);
protected halService: HALEndpointService,) {
super(route, router, items, authService, authorizationService, registryService, halService);
}

/*** AoT inheritance fix, will hopefully be resolved in the near future **/
async ngOnInit(): Promise<void> {
await super.ngOnInit();
ngOnInit(): void {
super.ngOnInit();
this.metadata$ = this.itemRD$.pipe(
map((rd: RemoteData<Item>) => rd.payload),
filter((item: Item) => hasValue(item)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-transla
import { TranslateLoaderMock } from '../../../../../shared/mocks/translate-loader.mock';
import { RouterTestingModule } from '@angular/router/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HALEndpointService } from '../../../../../core/shared/hal-endpoint.service';

describe('FileDescriptionComponent', () => {
let component: FileDescriptionComponent;
let fixture: ComponentFixture<FileDescriptionComponent>;
let translateService: TranslateService;
let halService: HALEndpointService;

beforeEach(async () => {
const configurationDataService = jasmine.createSpyObj('configurationDataService', {
Expand All @@ -27,6 +29,10 @@ describe('FileDescriptionComponent', () => {
}))
});

halService = jasmine.createSpyObj('authService', {
getRootHref: 'root url',
});

translateService = getMockTranslateService();
await TestBed.configureTestingModule({
imports: [TranslateModule.forRoot({
Expand All @@ -37,7 +43,8 @@ describe('FileDescriptionComponent', () => {
}), RouterTestingModule.withRoutes([]), BrowserAnimationsModule],
declarations: [FileDescriptionComponent],
providers: [
{ provide: ConfigurationDataService, useValue: configurationDataService }
{ provide: ConfigurationDataService, useValue: configurationDataService },
{ provide: HALEndpointService, useValue: halService }
]
}).compileComponents();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import { Component, Input, OnInit } from '@angular/core';
import { Component, Input } from '@angular/core';
import { MetadataBitstream } from 'src/app/core/metadata/metadata-bitstream.model';
import { getBaseUrl } from '../../../../../shared/clarin-shared-util';
import { ConfigurationProperty } from '../../../../../core/shared/configuration-property.model';
import { ConfigurationDataService } from '../../../../../core/data/configuration-data.service';
import { HALEndpointService } from '../../../../../core/shared/hal-endpoint.service';

const allowedPreviewFormats = ['text/plain', 'text/html', 'application/zip'];
@Component({
selector: 'ds-file-description',
templateUrl: './file-description.component.html',
styleUrls: ['./file-description.component.scss'],
})
export class FileDescriptionComponent implements OnInit {
export class FileDescriptionComponent {
@Input()
fileInput: MetadataBitstream;

/**
* UI URL loaded from the server.
*/
baseUrl = '';

constructor(protected configurationService: ConfigurationDataService) { }

async ngOnInit(): Promise<void> {
await this.assignBaseUrl();
}
constructor(protected halService: HALEndpointService) { }

public downloadFiles() {
window.location.href = this.baseUrl.replace('/server','') + `${this.fileInput.href}`;
console.log('${this.fileInput.href}', `${this.fileInput.href}`);
window.location.href = this.halService.getRootHref().replace('/server/api', '') + `${this.fileInput.href}`;
}

public isTxt() {
Expand All @@ -50,14 +40,4 @@ export class FileDescriptionComponent implements OnInit {

return allowedPreviewFormats.includes(this.fileInput.format);
}

/**
* Load base url from the configuration from the BE.
*/
async assignBaseUrl() {
this.baseUrl = await getBaseUrl(this.configurationService)
.then((baseUrlResponse: ConfigurationProperty) => {
return baseUrlResponse?.values?.[0];
});
}
}
21 changes: 21 additions & 0 deletions src/app/item-page/simple/item-page.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@
<ds-listable-object-component-loader *ngIf="!item.isWithdrawn || (isAdmin$|async)" [object]="item" [viewMode]="viewMode"></ds-listable-object-component-loader>
<ds-item-versions class="mt-2" [item]="item" [displayActions]="false"></ds-item-versions>
<ds-clarin-license-info class="mt-3 d-block" [item]="item"></ds-clarin-license-info>
<h6><i class="fa fa-paperclip">&nbsp;</i>Files in this item</h6>
<div class="pb-3">
<span class="pr-1">
<a class="btn btn-download" (click)="setCommandline()" style="text-decoration: none"
*ngIf="canShowCurlDownload">
<i class="fa fa-download fa-3x" style="display: block">&nbsp;</i>
Download instructions for command line
</a>
</span>
<div id="command-div" *ngIf="isCommandLineVisible">
<button class="repo-copy-btn pull-right" data-clipboard-target="#command-div"></button>
<pre style="background-color: #d9edf7; color: #3a87ad">{{ command }}</pre>
</div>
<span>
<a *ngIf="canDownloadAllFiles" class="btn btn-download" id="download-all-button" (click)="downloadFiles()"
style="visibility: visible">
<i style="display: block" class="fa fa-download fa-3x">&nbsp;</i>
Download all files in item ({{ totalFileSizes }})
</a>
</span>
</div>
<ds-preview-section [item]="item"></ds-preview-section>
</div>
</div>
Expand Down
9 changes: 7 additions & 2 deletions src/app/item-page/simple/item-page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { MetadataSchemaDataService } from 'src/app/core/data/metadata-schema-dat
import { MetadataFieldDataService } from 'src/app/core/data/metadata-field-data.service';
import { MetadataBitstreamDataService } from 'src/app/core/data/metadata-bitstream-data.service';
import { getMockTranslateService } from 'src/app/shared/mocks/translate.service.mock';
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';

const mockItem: Item = Object.assign(new Item(), {
bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])),
Expand All @@ -51,6 +51,7 @@ describe('ItemPageComponent', () => {
let authService: AuthService;
let translateService: TranslateService;
let registryService: RegistryService;
let halService: HALEndpointService;
const authorizationService = jasmine.createSpyObj('authorizationService', [
'isAuthorized',
]);
Expand Down Expand Up @@ -90,6 +91,10 @@ describe('ItemPageComponent', () => {
}))
});

halService = jasmine.createSpyObj('authService', {
getRootHref: 'root url',
});

TestBed.configureTestingModule({
imports: [TranslateModule.forRoot({
loader: {
Expand All @@ -112,7 +117,7 @@ describe('ItemPageComponent', () => {
{ provide: MetadataBitstreamDataService, useValue: mockMetadataBitstreamDataService },
RegistryService,
{ provide: AuthorizationDataService, useValue: authorizationDataService },
{ provide: ConfigurationDataService, useValue: configurationDataService }
{ provide: HALEndpointService, useValue: halService }
],

schemas: [NO_ERRORS_SCHEMA]
Expand Down
41 changes: 16 additions & 25 deletions src/app/item-page/simple/item-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import {
import { ViewMode } from '../../core/shared/view-mode.model';
import { AuthService } from '../../core/auth/auth.service';
import { getItemPageRoute } from '../item-page-routing-paths';
import { isNotEmpty } from '../../shared/empty.util';
import { isNotEmpty} from '../../shared/empty.util';
import { FeatureID } from '../../core/data/feature-authorization/feature-id';
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
import { redirectOn4xx } from '../../core/shared/authorized.operators';
import { RegistryService } from 'src/app/core/registry/registry.service';
import { MetadataBitstream } from 'src/app/core/metadata/metadata-bitstream.model';
import { Observable } from 'rxjs';
import { getBaseUrl } from '../../shared/clarin-shared-util';
import { ConfigurationProperty } from '../../core/shared/configuration-property.model';
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
import { Observable} from 'rxjs';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';

/**
* This component renders a simple item page.
Expand Down Expand Up @@ -67,7 +65,7 @@ export class ItemPageComponent implements OnInit {
/**
* determine to show download all zip button or not
*/
canDownloadAllFiles = true;
canDownloadAllFiles = false;
/**
* command for the download command feature
*/
Expand Down Expand Up @@ -103,10 +101,7 @@ export class ItemPageComponent implements OnInit {

itemUrl: string;

/**
* UI URL loaded from the server.
*/
baseUrl = '';
canShowCurlDownload = false;

constructor(
protected route: ActivatedRoute,
Expand All @@ -115,15 +110,14 @@ export class ItemPageComponent implements OnInit {
private authService: AuthService,
private authorizationService: AuthorizationDataService,
protected registryService: RegistryService,
protected configurationService: ConfigurationDataService
protected halService: HALEndpointService,
) {
}

/**
* Initialize instance variables
*/
async ngOnInit(): Promise<void> {
await this.assignBaseUrl();
ngOnInit(): void {
this.itemRD$ = this.route.data.pipe(
map((data) => data.dso as RemoteData<Item>),
redirectOn4xx(this.router, this.authService)
Expand Down Expand Up @@ -215,29 +209,26 @@ export class ItemPageComponent implements OnInit {

generateCurlCommand() {
const fileNames = this.listOfFiles.map((file: MetadataBitstream) => {
if (!file.canPreview) {
// Show `Download All Files` only if there are more files.
if (file.canPreview && this.listOfFiles.length > 1) {
this.canDownloadAllFiles = file.canPreview;
}

if (file.canPreview) {
this.canShowCurlDownload = true;
}

return file.name;
});

this.command = `curl --remote-name-all` + this.baseUrl + `/bitstream/handle/${
this.command = `curl --remote-name-all ` + this.halService.getRootHref() + `/core/bitstreams/handle/${
this.itemHandle
}/{${fileNames.join(',')}}`;
}

downloadFiles() {
window.location.href = this.baseUrl + `/bitstream/allzip?handleId=${this.itemHandle}`;
window.location.href = this.halService.getRootHref() + `/core/bitstreams/allzip?handleId=${this.itemHandle}`;
}

/**
* Load base url from the configuration from the BE.
*/
async assignBaseUrl() {
this.baseUrl = await getBaseUrl(this.configurationService)
.then((baseUrlResponse: ConfigurationProperty) => {
return baseUrlResponse?.values?.[0];
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@
{{"item.page.link.full" | translate}}
</a>
</div>
<ds-clarin-files-item-field [item]="object"
[iconName]="'fa-paperclip'"
[separator]="'<br />'">
</ds-clarin-files-item-field>
<!-- <ds-clarin-files-item-field [item]="object"-->
<!-- [iconName]="'fa-paperclip'"-->
<!-- [separator]="'<br />'">-->
<!-- </ds-clarin-files-item-field>-->
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
{{"item.page.link.full" | translate}}
</a>
</div>
<ds-clarin-files-item-field [item]="object"
[iconName]="'fa-paperclip'"
[separator]="'<br />'">
</ds-clarin-files-item-field>
<!-- <ds-clarin-files-item-field [item]="object"-->
<!-- [iconName]="'fa-paperclip'"-->
<!-- [separator]="'<br />'">-->
<!-- </ds-clarin-files-item-field>-->
</div>
</div>

0 comments on commit af3c35e

Please sign in to comment.