@@ -6,16 +6,18 @@ import path from 'path'
6
6
7
7
config ( )
8
8
9
- const BASE_URL = 'https://api.github.com/repos/VueTorrent/VueTorrent/releases/'
10
- const STABLE_BRANCH_NAME = 'latest'
11
- const DEV_BRANCH_NAME = 'tags/latest_nightly'
9
+
10
+ const BASE_URL_VERSION = 'https://api.github.com/repos/VueTorrent/VueTorrent/contents/version.txt?ref='
11
+ const BASE_URL_ZIPBALL = 'https://api.github.com/repos/VueTorrent/VueTorrent/zipball/'
12
+ const STABLE_BRANCH_NAME = 'latest-release'
13
+ const DEV_BRANCH_NAME = 'nightly-release'
12
14
const VERSION_PATTERN = / ^ v ? (?< version > [ 0 - 9 . ] + ) ( - (?< commits > \d + ) - g (?< sha > [ 0 - 9 a - f ] + ) ) ? $ /
13
15
14
16
const BASE_FS_PATH = process . env . VUETORRENT_PATH || '/vuetorrent'
15
- const TEMP_ZIP_PATH = `${ BASE_FS_PATH } /vuetorrent.zip`
16
- const WEBUI_PATH = `${ BASE_FS_PATH } /vuetorrent`
17
- const VERSION_FILE_PATH = `${ WEBUI_PATH } /version.txt`
18
- const WEBUI_OLD_PATH = `${ BASE_FS_PATH } /vuetorrent-old`
17
+ const TEMP_ZIP_PATH = `${ BASE_FS_PATH } /vuetorrent.zip`
18
+ const WEBUI_PATH = `${ BASE_FS_PATH } /vuetorrent`
19
+ const VERSION_FILE_PATH = `${ WEBUI_PATH } /version.txt`
20
+ const WEBUI_OLD_PATH = `${ BASE_FS_PATH } /vuetorrent-old`
19
21
20
22
function extractVersion ( version ) {
21
23
const match = version . match ( VERSION_PATTERN )
@@ -24,8 +26,8 @@ function extractVersion(version) {
24
26
25
27
function formatVersion ( version ) {
26
28
if ( ! version ) return 'unknown'
27
- if ( ! version . commits || ! version . sha ) return `v${ version . version } `
28
- return `v${ version . version } -${ version . commits } -g${ version . sha } `
29
+ if ( ! version . commits || ! version . sha ) return `v${ version . version } `
30
+ return `v${ version . version } -${ version . commits } -g${ version . sha } `
29
31
}
30
32
31
33
function getInstalledVersion ( ) {
@@ -47,10 +49,8 @@ async function getLatestVersion(url) {
47
49
}
48
50
} )
49
51
50
- return {
51
- latestVersion : extractVersion ( response . data . name ) ,
52
- download_url : response . data . assets [ 0 ] . browser_download_url
53
- }
52
+ // Extract from base64
53
+ return extractVersion ( atob ( response . data . content . trim ( ) ) )
54
54
}
55
55
56
56
async function downloadFile ( url , outputPath ) {
@@ -74,7 +74,8 @@ async function unzipFile(zipPath, extractTo) {
74
74
const zip = await JSZip . loadAsync ( data )
75
75
await Promise . all ( Object . keys ( zip . files ) . map ( async ( filename ) => {
76
76
const file = zip . files [ filename ]
77
- const filePath = path . join ( extractTo , filename )
77
+ const newName = filename . slice ( filename . indexOf ( '/' ) )
78
+ const filePath = path . join ( extractTo , newName )
78
79
if ( file . dir ) {
79
80
fs . mkdirSync ( filePath , { recursive : true } )
80
81
} else {
@@ -102,7 +103,7 @@ async function downloadUpdate(link) {
102
103
103
104
// Unzip the downloaded file
104
105
try {
105
- await unzipFile ( TEMP_ZIP_PATH , BASE_FS_PATH )
106
+ await unzipFile ( TEMP_ZIP_PATH , WEBUI_PATH )
106
107
} catch ( err ) {
107
108
console . error ( err )
108
109
// Restore backup if unzip fails
@@ -113,25 +114,27 @@ async function downloadUpdate(link) {
113
114
}
114
115
115
116
export async function checkForUpdate ( ) {
116
- let url
117
+ let versionUrl , downloadUrl
117
118
switch ( process . env . RELEASE_TYPE ) {
118
119
case 'dev' :
119
- url = BASE_URL + DEV_BRANCH_NAME
120
+ versionUrl = BASE_URL_VERSION + DEV_BRANCH_NAME
121
+ downloadUrl = BASE_URL_ZIPBALL + DEV_BRANCH_NAME
120
122
break
121
123
case 'stable' :
122
124
default :
123
- url = BASE_URL + STABLE_BRANCH_NAME
125
+ versionUrl = BASE_URL_VERSION + STABLE_BRANCH_NAME
126
+ downloadUrl = BASE_URL_ZIPBALL + STABLE_BRANCH_NAME
124
127
break
125
128
}
126
129
127
130
const installedVersion = getInstalledVersion ( )
128
- const { latestVersion, download_url } = await getLatestVersion ( url )
131
+ const latestVersion = await getLatestVersion ( versionUrl )
129
132
130
133
if ( installedVersion ?. version !== latestVersion ?. version
131
- || installedVersion ?. commits !== latestVersion ?. commits
132
- || installedVersion ?. sha !== latestVersion ?. sha ) {
133
- await downloadUpdate ( download_url )
134
- return `Update successful from ${ formatVersion ( installedVersion ) } to ${ formatVersion ( latestVersion ) } `
134
+ || installedVersion ?. commits !== latestVersion ?. commits
135
+ || installedVersion ?. sha !== latestVersion ?. sha ) {
136
+ await downloadUpdate ( downloadUrl )
137
+ return `Update successful from ${ formatVersion ( installedVersion ) } to ${ formatVersion ( latestVersion ) } `
135
138
}
136
139
return 'Already using the latest version'
137
140
}
0 commit comments