Skip to content

Commit

Permalink
Fix request downloading files + store building error
Browse files Browse the repository at this point in the history
  • Loading branch information
DantSu committed Dec 22, 2024
1 parent 3a15a72 commit f7f8115
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 182 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Total Download](https://img.shields.io/github/downloads/DantSu/Telmi-Sync/total.svg) [![v0.7.2 download](https://img.shields.io/github/downloads/DantSu/Telmi-Sync/0.7.2/total.svg)](https://github.com/DantSu/Telmi-Sync/releases/tag/0.7.2)
![Total Download](https://img.shields.io/github/downloads/DantSu/Telmi-Sync/total.svg) [![v0.7.3 download](https://img.shields.io/github/downloads/DantSu/Telmi-Sync/0.7.3/total.svg)](https://github.com/DantSu/Telmi-Sync/releases/tag/0.7.3)

<p align="center"><img = src="https://dantsu.com/files/Telmi_MiyooPC.jpg" alt="Telmi OS - Telmi Sync" /></p>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "telmi-sync",
"description": "Desktop application for synchronize stories and music with Telmi OS.",
"version": "0.7.2",
"version": "0.7.3",
"author": {
"name": "Franck ALARY",
"url": "http://dantsu.com/"
Expand Down
87 changes: 28 additions & 59 deletions public/MainEvents/Helpers/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@ import * as fs from 'fs'

const
defaultHeader = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'keep-alive',
'language': 'fr',
'User-Agent': 'TelmiSync/0.7.2 ( https://github.com/DantSu/Telmi-Sync )',
'User-Agent': 'TelmiSync/0.7.3 ( https://github.com/DantSu/Telmi-Sync )',
},

htmlTag = '<!DOCTYPE html>',

downloadRangeFile = (url, filePath, resolve, reject, onProgress, bytesStart, bytesLength) => {
if (bytesStart !== undefined && bytesStart > bytesLength) {
return resolve(filePath)
}

downloadRangeFile = (url, filePath, resolve, reject, onProgress, bytesStart) => {
try {
const
isHttps = url.substring(0, 6) === 'https:',
request = isHttps ? requestHttps : requestHttp,
bytesEnd = bytesStart !== undefined ? (bytesStart + 268435456) : undefined,
file = fs.createWriteStream(filePath, {flags: 'a'}),
req = request(
url,
{
method: 'GET',
rejectUnauthorized: false,
timeout: 5000,
headers: Object.assign(
{...defaultHeader},
bytesStart !== undefined ? {'Range': 'bytes=' + bytesStart + '-' + (bytesEnd - 1)} : null
bytesStart > 0 ? {'Range': 'bytes=' + bytesStart + '-' + (bytesStart + 268435455)} : null
)
},
(res) => {
Expand All @@ -54,29 +48,42 @@ const
}
}

let fileProgress = bytesStart || 0

const bytesLength = res.headers['content-length']
let bytesLengthDownloaded = bytesStart
res.on('data', (d) => {
fileProgress += d.length
onProgress(Math.round(fileProgress / (bytesLength || fileProgress) * 100), 100)
bytesLengthDownloaded += d.length
onProgress(Math.round(bytesLengthDownloaded / (bytesLength || bytesLengthDownloaded) * 100), 100)
})

res.pipe(file)
file.on('finish', () => {
file.close()
if (fileProgress > 512000) {
return downloadRangeFile(url, filePath, resolve, reject, onProgress, bytesEnd || 2, bytesLength || 1)

const downloadNextRange = () => {
if (res.headers['accept-ranges'] !== 'bytes' || bytesLength === undefined) {
return resolve(filePath)
}
if (bytesLengthDownloaded >= bytesLength) {
return resolve(filePath)
}
downloadRangeFile(url, filePath, resolve, reject, onProgress, bytesLengthDownloaded)
}

if (bytesLengthDownloaded > 64000) {
return downloadNextRange()
}
const content = fs.readFileSync(filePath).toString('utf8')
if (content.substring(0, htmlTag.length) !== htmlTag) {
return downloadRangeFile(url, filePath, resolve, reject, onProgress, bytesEnd || 2, bytesLength || 1)
return downloadNextRange()
}
const startPos = content.toLowerCase().indexOf(' url=') + 5
if (startPos === -1) {
return downloadRangeFile(url, filePath, resolve, reject, onProgress, bytesEnd || 2, bytesLength || 1)
if (startPos === 4) {
return downloadNextRange()
}
const endPos = content.indexOf('"', startPos)
if (endPos === -1) {
return downloadRangeFile(url, filePath, resolve, reject, onProgress, bytesEnd || 2, bytesLength || 1)
return downloadNextRange()
}
return downloadFile(content.substring(startPos, endPos), filePath, onProgress)
.then((filePath) => resolve(filePath))
Expand All @@ -103,45 +110,7 @@ const
if (fs.existsSync(filePath)) {
fs.rmSync(filePath)
}

const
isHttps = url.substring(0, 6) === 'https:',
request = isHttps ? requestHttps : requestHttp,
req = request(
url,
{
method: 'HEAD',
timeout: 5000,
},
(res) => {
if (res.statusCode < 200 || res.statusCode >= 400) {
return reject(new Error('statusCode=' + res.statusCode + ' : ' + url))
}

if (res.statusCode >= 300 && res.statusCode < 400) {
if (typeof res.headers['location'] === 'string' && res.headers['location'] !== url) {
return downloadFile(res.headers['location'], filePath, onProgress)
.then((filePath) => resolve(filePath))
.catch((e) => reject(e))
} else {
return reject(new Error('statusCode=' + res.statusCode + ' : ' + url))
}
}

if (res.headers['accept-ranges'] === 'bytes' && res.headers['content-length'] !== undefined) {
downloadRangeFile(url, filePath, resolve, reject, onProgress, 0, res.headers['content-length'])
} else {
downloadRangeFile(url, filePath, resolve, reject, onProgress)
}
}
)
req.on('error', e => {
reject(e)
})
req.on('timeout', () => {
reject(new Error('Unable to connect to : ' + url))
})
req.end()
downloadRangeFile(url, filePath, resolve, reject, onProgress, 0)
})
},

Expand Down
Loading

0 comments on commit f7f8115

Please sign in to comment.