Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display an image whose url requires authentication #112

Open
hmsuchier opened this issue Oct 29, 2021 · 2 comments
Open

Display an image whose url requires authentication #112

hmsuchier opened this issue Oct 29, 2021 · 2 comments

Comments

@hmsuchier
Copy link

Hi,
Is there a way to display images whose url requires authentication ? In a normal context I use a pipe to catch query and attach authentication token, but is there a way to do such thing with the current lib ?
Thanks !

@themyth92
Copy link
Owner

Short anwser, NO. I would suggest u can attach the auth token in the image url and let your server to return the image if the auth is correct

@oneplugin
Copy link

oneplugin commented Sep 18, 2023

Use this component

@component({
selector: 'app-secured-image',
template: <img [src]="dataUrl$|async"/>
})
export class SecuredImageComponent implements OnInit {
@input() private attachment: Attachment = {};

private src$ = new BehaviorSubject(this.attachment);
private url :Observable = undefined

constructor(private httpClient: HttpClient, private domSanitizer: DomSanitizer) {}

ngOnInit(): void {}

ngOnChanges(): void {
this.src$.next(this.attachment);
}

dataUrl$ = this.src$.pipe(switchMap(attachment => this.loadImage(attachment)))

private loadImage(attachment: Attachment): Observable {
let params = {"id":attachment.id,"fileName":attachment.fileName,"objectPath":attachment.storagePath,"bucketName":"unknot"}
const httpOptions : Object = {
headers: new HttpHeaders({'Accept':'image/png'}),
responseType: 'blob'
};
this.url = this.httpClient.post(${environment.serverUrl}/file/download,JSON.stringify(params),httpOptions)
.pipe(map(e => this.domSanitizer.bypassSecurityTrustUrl(URL.createObjectURL(e)) as string))

return this.url

}
}

@ViewChildren('albumImages',{read: ElementRef}) albumImages!: QueryList

open handler
open(index){
const albumImages = [];
if( this.albumImages.length > 0 ){
for( const albumImage of this.albumImages){
const img = albumImage.nativeElement.querySelector("img")
if( img && img.src != undefined){
const santizedUrl = this.domSanitizer.bypassSecurityTrustUrl(img.src)
albumImages.push({
src: santizedUrl,
caption: "",
thumb: santizedUrl
})
}
this.lightbox.open(albumImages, index,{showImageNumberLabel:true,centerVertically:true,wrapAround:true,disableScrolling:true});
}
}
}

Works like charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants