Parcourir la source

feat(Downloaders): download when local and remote file sizes mismatch

Evshiron Magicka il y a 8 ans
Parent
commit
e43a57dacd
3 fichiers modifiés avec 34 ajouts et 9 suppressions
  1. 4 4
      src/lib/Downloader.ts
  2. 26 1
      src/lib/DownloaderBase.ts
  3. 4 4
      src/lib/FFmpegDownloader.ts

+ 4 - 4
src/lib/Downloader.ts

@@ -61,10 +61,12 @@ export class Downloader extends DownloaderBase {
         debug('in fetch', 'filename', filename);
         debug('in fetch', 'path', path);
 
-        if(!(await this.isFileExists(path))) {
-            await this.download(url, filename, path, showProgress);
+        if(await this.isFileExists(path) && await this.isFileSynced(url, path)) {
+            return path;
         }
 
+        await this.download(url, filename, path, showProgress);
+
         return path;
 
     }
@@ -83,7 +85,6 @@ export class Downloader extends DownloaderBase {
     }
 
     protected extensionByPlatform(platform: string) {
-
         switch(platform) {
         case 'win32':
         case 'win':
@@ -97,7 +98,6 @@ export class Downloader extends DownloaderBase {
         default:
             throw new Error('ERROR_UNKNOWN_PLATFORM');
         }
-
     }
 
 }

+ 26 - 1
src/lib/DownloaderBase.ts

@@ -3,7 +3,7 @@ import { dirname, basename, resolve } from 'path';
 
 import * as request from 'request';
 import * as ProgressBar from 'progress';
-import { ensureDirSync, exists, writeFile } from 'fs-extra-promise';
+import { ensureDirSync, exists, lstatAsync, writeFile } from 'fs-extra-promise';
 
 const debug = require('debug')('build:downloader');
 const progress = require('request-progress');
@@ -100,12 +100,37 @@ export abstract class DownloaderBase {
 
     }
 
+    protected getLocalSize(path: string): Promise<number> {
+        return lstatAsync(path)
+        .then(stat => stat.size);
+    }
+
+    protected getRemoteSize(url: string): Promise<number> {
+        return new Promise((resolve, reject) => {
+            request.head(url)
+            .on('error', reject)
+            .on('response', res => resolve(parseInt(res.headers['content-length'], 10)));
+        });
+    }
+
     protected isFileExists(path: string) {
         return new Promise((resolve, reject) => {
             exists(path, resolve);
         });
     }
 
+    protected async isFileSynced(url: string, path: string) {
+
+        const localSize = await this.getLocalSize(path);
+        const remoteSize = await this.getRemoteSize(url);
+
+        debug('in isFileSynced', 'localSize', localSize);
+        debug('in isFileSynced', 'remoteSize', remoteSize);
+
+        return localSize == remoteSize;
+
+    }
+
     protected async download(url: string, filename: string, path: string, showProgress: boolean) {
 
         let bar: ProgressBar = null;

+ 4 - 4
src/lib/FFmpegDownloader.ts

@@ -73,16 +73,17 @@ export class FFmpegDownloader extends DownloaderBase {
         debug('in fetch', 'filename', filename);
         debug('in fetch', 'path', path);
 
-        if(!(await this.isFileExists(path))) {
-            await this.download(url, filename, path, showProgress);
+        if(await this.isFileExists(path) && await this.isFileSynced(url, path)) {
+            return path;
         }
 
+        await this.download(url, filename, path, showProgress);
+
         return path;
 
     }
 
     protected async handleVersion(version: string) {
-
         switch(version) {
         case 'lts':
         case 'stable':
@@ -91,7 +92,6 @@ export class FFmpegDownloader extends DownloaderBase {
         default:
             return version[0] == 'v' ? version.slice(1) : version;
         }
-
     }
 
 }