Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(representation): add support for uploading and viewing archive f…
…iles (DEV-18) (#600) * feat(archive): adds support for uploading and viewing archive files * test(archive): adds test to archive comp * test(archive): fixes import
- Loading branch information
Showing
17 changed files
with
248 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/app/workspace/resource/representation/archive/archive.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div *ngIf="src && src.fileValue.fileUrl"> | ||
<button class="download" mat-button (click)="downloadArchive(src.fileValue.fileUrl)"> | ||
<mat-icon> | ||
file_download | ||
</mat-icon> | ||
Click to download | ||
</button> | ||
</div> | ||
<div *ngIf="!src || !src.fileValue.fileUrl"> | ||
No valid file url found for this resource. | ||
</div> | ||
|
Empty file.
110 changes: 110 additions & 0 deletions
110
src/app/workspace/resource/representation/archive/archive.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { KnoraApiConnection } from '@dasch-swiss/dsp-js'; | ||
import { HarnessLoader } from '@angular/cdk/testing'; | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { Component, OnInit, ViewChild } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { MatButtonHarness } from '@angular/material/button/testing'; | ||
import { MatDialogModule } from '@angular/material/dialog'; | ||
import { MatSnackBarModule } from '@angular/material/snack-bar'; | ||
import { AppInitService } from 'src/app/app-init.service'; | ||
import { DspApiConfigToken, DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens'; | ||
import { TestConfig } from 'test.config'; | ||
import { FileRepresentation } from '../file-representation'; | ||
|
||
import { ArchiveComponent } from './archive.component'; | ||
|
||
const archiveFileValue = { | ||
'arkUrl': 'http://0.0.0.0:3336/ark:/72163/1/0123/6c=f69h6Ss6GXPME565EqAS/dDHcFHlwQ9K46255QfUGrQ8', | ||
'attachedToUser': 'http://rdfh.ch/users/root', | ||
'fileUrl': 'http://0.0.0.0:1024/0123/Eu71soNXOAL-DVweVgODkFh.zip/file', | ||
'filename': 'Eu71soNXOAL-DVweVgODkFh.zip', | ||
'hasPermissions': 'CR knora-admin:ProjectAdmin|D knora-admin:ProjectAdmin|M knora-admin:ProjectAdmin|V knora-admin:ProjectAdmin|RV knora-admin:ProjectAdmin', | ||
'id': 'http://rdfh.ch/0123/6c-f69h6Ss6GXPME565EqA/values/dDHcFHlwQ9K46255QfUGrQ', | ||
'property': 'http://api.knora.org/ontology/knora-api/v2#hasArchiveFileValue', | ||
'propertyComment': 'Connects a Representation to a zip archive', | ||
'propertyLabel': 'hat Zip', | ||
'strval': 'http://0.0.0.0:1024/0123/Eu71soNXOAL-DVweVgODkFh.zip/file', | ||
'type': 'http://api.knora.org/ontology/knora-api/v2#ArchiveFileValue', | ||
'userHasPermission': 'CR', | ||
'uuid': 'dDHcFHlwQ9K46255QfUGrQ', | ||
'valueCreationDate': '2021-12-03T09:59:46.609839Z', | ||
'valueHasComment': undefined, | ||
'versionArkUrl': 'http://0.0.0.0:3336/ark:/72163/1/0123/6c=f69h6Ss6GXPME565EqAS/dDHcFHlwQ9K46255QfUGrQ8.20211203T095946609839Z' | ||
}; | ||
|
||
@Component({ | ||
template: ` | ||
<app-archive [src]="archiveFileRepresentation"> | ||
</app-archive>` | ||
}) | ||
class TestHostComponent implements OnInit { | ||
|
||
@ViewChild(ArchiveComponent) archiveComp: ArchiveComponent; | ||
|
||
archiveFileRepresentation: FileRepresentation; | ||
|
||
ngOnInit() { | ||
|
||
this.archiveFileRepresentation = new FileRepresentation(archiveFileValue); | ||
} | ||
} | ||
|
||
describe('ArchiveComponent', () => { | ||
let testHostComponent: TestHostComponent; | ||
let testHostFixture: ComponentFixture<TestHostComponent>; | ||
let loader: HarnessLoader; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [ | ||
ArchiveComponent, | ||
TestHostComponent | ||
], | ||
imports: [ | ||
HttpClientTestingModule, | ||
MatDialogModule, | ||
MatSnackBarModule | ||
], | ||
providers: [ | ||
AppInitService, | ||
{ | ||
provide: DspApiConfigToken, | ||
useValue: TestConfig.ApiConfig | ||
}, | ||
{ | ||
provide: DspApiConnectionToken, | ||
useValue: new KnoraApiConnection(TestConfig.ApiConfig) | ||
} | ||
] | ||
}) | ||
.compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
testHostFixture = TestBed.createComponent(TestHostComponent); | ||
testHostComponent = testHostFixture.componentInstance; | ||
loader = TestbedHarnessEnvironment.loader(testHostFixture); | ||
testHostFixture.detectChanges(); | ||
expect(testHostComponent).toBeTruthy(); | ||
}); | ||
|
||
it('should have a file url', () => { | ||
expect(testHostComponent.archiveFileRepresentation.fileValue.fileUrl).toEqual('http://0.0.0.0:1024/0123/Eu71soNXOAL-DVweVgODkFh.zip/file'); | ||
}); | ||
|
||
it('should show a download button if the file url is provided', async () => { | ||
const downloadButtonElement = await loader.getHarness(MatButtonHarness.with({ selector: '.download' })); | ||
|
||
expect(downloadButtonElement).toBeTruthy(); | ||
}); | ||
|
||
it('should NOT show a download button if the file url is NOT provided', async () => { | ||
testHostComponent.archiveFileRepresentation = undefined; | ||
testHostFixture.detectChanges(); | ||
|
||
const downloadButtonElement = await loader.getAllHarnesses(MatButtonHarness.with({ selector: '.download' })); | ||
|
||
expect(downloadButtonElement.length).toEqual(0); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/app/workspace/resource/representation/archive/archive.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Component, Input, OnInit } from '@angular/core'; | ||
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service'; | ||
import { FileRepresentation } from '../file-representation'; | ||
|
||
@Component({ | ||
selector: 'app-archive', | ||
templateUrl: './archive.component.html', | ||
styleUrls: ['./archive.component.scss'] | ||
}) | ||
export class ArchiveComponent implements OnInit { | ||
|
||
@Input() src: FileRepresentation; | ||
|
||
constructor( | ||
private readonly _http: HttpClient, | ||
private _errorHandler: ErrorHandlerService | ||
) { } | ||
|
||
ngOnInit(): void { } | ||
|
||
// https://stackoverflow.com/questions/66986983/angular-10-download-file-from-firebase-link-without-opening-into-new-tab | ||
async downloadArchive(url: string) { | ||
try { | ||
const res = await this._http.get(url, { responseType: 'blob' }).toPromise(); | ||
this.downloadFile(res); | ||
} catch (e) { | ||
this._errorHandler.showMessage(e); | ||
} | ||
} | ||
|
||
downloadFile(data) { | ||
const url = window.URL.createObjectURL(data); | ||
const e = document.createElement('a'); | ||
e.href = url; | ||
e.download = url.substr(url.lastIndexOf('/') + 1); | ||
document.body.appendChild(e); | ||
e.click(); | ||
document.body.removeChild(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.