UNPKG

schoolx-ota-manager

Version:

React Native library for managing OTA updates with GitLab integration

60 lines 2.43 kB
import axios from "axios"; import RNFS from "react-native-fs"; export class GitLabService { constructor(token, baseUrl, projectId, repoFolder) { this.token = token; this.baseUrl = baseUrl; this.projectId = projectId; this.repoFolder = repoFolder; } encodeFilePath(path) { return path.replace(/\//g, "%2F"); } async getVersion() { try { const filePath = this.encodeFilePath(`${this.repoFolder}/version.json`); const response = await axios.get(`${this.baseUrl}/projects/${this.projectId}/repository/files/${filePath}/raw?ref=main`, { headers: { "PRIVATE-TOKEN": this.token, }, }); return response.data; } catch (error) { console.error("Get version failed:", error); throw error; } } async downloadPlatformBundle(platform) { const platformFolder = platform === "ios" ? "ios" : "android"; const filePath = this.encodeFilePath(`${this.repoFolder}/${platformFolder}/hermes.${platform}.hbc.zip`); const fileName = `hermes.${platform}.hbc.zip`; try { const savePath = `${RNFS.CachesDirectoryPath}/${fileName}`; const response = await axios.get(`${this.baseUrl}/projects/${this.projectId}/repository/files/${filePath}/raw?ref=main`, { responseType: "arraybuffer", headers: { "PRIVATE-TOKEN": this.token, }, }); if (response.data) { const uint8Array = new Uint8Array(response.data); let binary = ""; const chunkSize = 0x8000; for (let i = 0; i < uint8Array.length; i += chunkSize) { binary += String.fromCharCode.apply(null, Array.from(uint8Array.subarray(i, i + chunkSize))); } const base64Data = btoa(binary); await RNFS.writeFile(savePath, base64Data, "base64"); console.log(`Downloaded bundle saved to: ${savePath}`); return savePath; } throw new Error("No data received from GitLab"); } catch (error) { console.error("Download platform bundle failed:", error); throw error; } } } //# sourceMappingURL=GitLabService.js.map