Skip to content

Commit 448d5c5

Browse files
committed
✨ add download files
1 parent f8a7693 commit 448d5c5

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

src/app/core/services/file.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ export class FileService {
3939
return resp.ok;
4040
}));
4141
}
42+
43+
downloadFile(fileID: string): Observable<Blob> {
44+
const url = `${base_url}/file/download/${fileID}`;
45+
return this.http.get(url, { responseType: 'blob', ...this.headers });
46+
}
4247
}

src/app/features/home/components/file/file.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
</div>
55
<span class="name">{{ file.name }}</span>
66
</div>
7-
<app-dots-dropdown class="actions"></app-dots-dropdown>
7+
<app-dots-dropdown class="actions" [file]="file"></app-dots-dropdown>

src/app/features/home/home.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<div class="fluid-container">
22
<app-actions [folder]="root"></app-actions>
3-
<div class="not-items" *ngIf="!root.folders?.length && !root.files?.length">
3+
<div class="not-items" *ngIf="root.folders.length < 1 && root.files.length < 1">
44
<img src="assets/images/no-files.svg" alt="no-files-image">
55
<span class="description">Agrega tu primer archivo o carpeta</span>
66
</div>
7-
<div class="files" *ngIf="root.folders.length && root.folders.length">
7+
<div class="files" *ngIf="root.folders.length > 1 || root.files.length > 1">
88
<section class="group" *ngIf="root.folders.length > 0">
99
<span class="title">Folders</span>
1010
<div class="items">

src/app/shared/dropdowns/dots-dropdown/dots-dropdown.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<i class="bx bx-palette"></i>
1111
<span class="title">Cambiar color</span>
1212
</li>
13-
<li role="menuitem" class='dropdown-item'>
13+
<li role="menuitem" class='dropdown-item' (click)="downloadFile()">
1414
<i class="bx bx-download"></i>
1515
<span class="title">Descargar</span>
1616
</li>
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
import { Component, ElementRef, ViewChild } from '@angular/core';
1+
import { Component, ElementRef, Input, ViewChild } from '@angular/core';
2+
3+
import { FileService } from '@services/file.service';
4+
5+
import { File } from '@models/file.model';
6+
import { Folder } from '@models/folder.model';
27

38
@Component({
49
selector: 'app-dots-dropdown',
510
templateUrl: './dots-dropdown.component.html',
611
styleUrls: ['./dots-dropdown.component.scss']
712
})
813
export class DotsDropdownComponent {
14+
@Input() file: File | Folder;
915
@ViewChild('dropdown') dropdown: ElementRef;
1016

17+
constructor(
18+
private fileService: FileService,
19+
) { }
20+
1121
changeVisibility() {
1222
this.dropdown.nativeElement.classList.toggle('show');
1323
}
24+
25+
downloadFile(): void {
26+
this.fileService.downloadFile(this.file._id as string).subscribe({
27+
next: (blob) => {
28+
const url = window.URL.createObjectURL(blob);
29+
const link = document.createElement('a');
30+
link.href = url;
31+
link.download = this.file.name.slice(0, this.file.name.lastIndexOf('.')) || this.file.name;
32+
link.click();
33+
},
34+
complete: () => {
35+
this.hideDropdown();
36+
}
37+
});
38+
}
39+
40+
hideDropdown(): void {
41+
this.dropdown.nativeElement.classList.remove('show');
42+
}
1443
}

0 commit comments

Comments
 (0)