From d5e6c8bf9833e268195a9283e065db4e1a110817 Mon Sep 17 00:00:00 2001
From: vkartk <53650724+vkartk@users.noreply.github.com>
Date: Fri, 26 Jan 2024 09:38:38 +0530
Subject: [PATCH 1/4] Backend: Integrate file size calculation and API endpoint
(#322)
---
app/ytdl.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/app/ytdl.py b/app/ytdl.py
index 16a4fff..8b05857 100644
--- a/app/ytdl.py
+++ b/app/ytdl.py
@@ -39,6 +39,7 @@ class DownloadInfo:
self.custom_name_prefix = custom_name_prefix
self.msg = self.percent = self.speed = self.eta = None
self.status = "pending"
+ self.size = None
self.timestamp = time.time_ns()
self.error = error
@@ -138,7 +139,9 @@ class Download:
return
self.tmpfilename = status.get('tmpfilename')
if 'filename' in status:
- self.info.filename = os.path.relpath(status.get('filename'), self.download_dir)
+ fileName = status['filename']
+ self.info.filename = os.path.relpath(fileName, self.download_dir)
+ self.info.size = os.path.getsize(fileName) if os.path.exists(fileName) else None
# Set correct file extension for thumbnails
if(self.info.format == 'thumbnail'):
From 3f4240a526fdebb0fe0805cc1a8818bc772175c0 Mon Sep 17 00:00:00 2001
From: vkartk <53650724+vkartk@users.noreply.github.com>
Date: Fri, 26 Jan 2024 09:39:39 +0530
Subject: [PATCH 2/4] Frontend: Implement file size display in Downloads
interface (#322)
---
ui/src/app/app.component.html | 3 +++
ui/src/app/app.module.ts | 3 ++-
ui/src/app/downloads.pipe.ts | 15 +++++++++++++++
3 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html
index e444fab..a47bb15 100644
--- a/ui/src/app/app.component.html
+++ b/ui/src/app/app.component.html
@@ -153,6 +153,7 @@
+
File Size |
|
|
|
@@ -176,6 +177,8 @@
Error: {{download.value.error}}
+
+ {{ download.value.size | fileSize }}
|
|
diff --git a/ui/src/app/app.module.ts b/ui/src/app/app.module.ts
index 35e0621..e944c83 100644
--- a/ui/src/app/app.module.ts
+++ b/ui/src/app/app.module.ts
@@ -7,7 +7,7 @@ import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { CookieService } from 'ngx-cookie-service';
import { AppComponent } from './app.component';
-import { EtaPipe, SpeedPipe, EncodeURIComponent } from './downloads.pipe';
+import { EtaPipe, SpeedPipe, EncodeURIComponent, FileSizePipe } from './downloads.pipe';
import { MasterCheckboxComponent, SlaveCheckboxComponent } from './master-checkbox.component';
import { MeTubeSocket } from './metube-socket';
import { NgSelectModule } from '@ng-select/ng-select';
@@ -18,6 +18,7 @@ import { ServiceWorkerModule } from '@angular/service-worker';
AppComponent,
EtaPipe,
SpeedPipe,
+ FileSizePipe,
EncodeURIComponent,
MasterCheckboxComponent,
SlaveCheckboxComponent
diff --git a/ui/src/app/downloads.pipe.ts b/ui/src/app/downloads.pipe.ts
index 55223c3..0ad7b78 100644
--- a/ui/src/app/downloads.pipe.ts
+++ b/ui/src/app/downloads.pipe.ts
@@ -44,3 +44,18 @@ export class EncodeURIComponent implements PipeTransform {
return encodeURIComponent(value);
}
}
+
+@Pipe({
+ name: 'fileSize'
+})
+export class FileSizePipe implements PipeTransform {
+ transform(value: number): string {
+ if (value === 0) return '0 Bytes';
+
+ const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
+ const unitIndex = Math.floor(Math.log(value) / Math.log(1000)); // Use 1000 for common units
+
+ const unitValue = value / Math.pow(1000, unitIndex);
+ return `${unitValue.toFixed(2)} ${units[unitIndex]}`;
+ }
+}
\ No newline at end of file
From feec0c56b460416b04d1694797242b304d872d62 Mon Sep 17 00:00:00 2001
From: vkartk <53650724+vkartk@users.noreply.github.com>
Date: Fri, 26 Jan 2024 09:48:44 +0530
Subject: [PATCH 3/4] Enhance FileSizePipe to handle NaN and zero bytes for
better resilience
---
ui/src/app/downloads.pipe.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/ui/src/app/downloads.pipe.ts b/ui/src/app/downloads.pipe.ts
index 0ad7b78..1287258 100644
--- a/ui/src/app/downloads.pipe.ts
+++ b/ui/src/app/downloads.pipe.ts
@@ -50,7 +50,7 @@ export class EncodeURIComponent implements PipeTransform {
})
export class FileSizePipe implements PipeTransform {
transform(value: number): string {
- if (value === 0) return '0 Bytes';
+ if (isNaN(value) || value === 0) return '0 Bytes';
const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const unitIndex = Math.floor(Math.log(value) / Math.log(1000)); // Use 1000 for common units
From f9a2a697df32719547fb7dfa09f40358e248ce24 Mon Sep 17 00:00:00 2001
From: vkartk <53650724+vkartk@users.noreply.github.com>
Date: Fri, 26 Jan 2024 14:04:47 +0530
Subject: [PATCH 4/4] Prevent crash on missing filename (status.get)
---
app/ytdl.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/ytdl.py b/app/ytdl.py
index 8b05857..6bb59fc 100644
--- a/app/ytdl.py
+++ b/app/ytdl.py
@@ -139,7 +139,7 @@ class Download:
return
self.tmpfilename = status.get('tmpfilename')
if 'filename' in status:
- fileName = status['filename']
+ fileName = status.get('filename')
self.info.filename = os.path.relpath(fileName, self.download_dir)
self.info.size = os.path.getsize(fileName) if os.path.exists(fileName) else None