Certainly! I can help you write a TypeScript script using Tauri API v2 to:
- Download a
.rar file from a given URL.
- Extract the contents of the
.rar file to a selected folder.
- Move the
players folder to the $CONFIG/Call of Duty Modern Warfare directory.
Below is a step-by-step guide to achieve this.
Prerequisites
- Tauri CLI and API v2: Ensure you have Tauri set up with API v2.
- Node.js and npm: Required for managing packages and building your Tauri app.
- Unrar Library: We'll use the
node-unrar-js package to extract .rar files.
Step 1: Install Necessary Packages
First, install the required npm packages:
npm install axios node-unrar-js
- axios: For downloading files.
- node-unrar-js: For extracting
.rar files.
Step 2: Download the .rar File
We'll use axios to download the file from the given URL.
import axios from 'axios';
import { writeBinaryFile } from '@tauri-apps/api/fs';
import { appDir } from '@tauri-apps/api/path';
async function downloadFile(url: string, outputPath: string) {
const response = await axios.get(url, { responseType: 'arraybuffer' });
await writeBinaryFile({ path: outputPath, contents: new Uint8Array(response.data) });
}
Explanation:
axios.get: Downloads the file as an array buffer.
writeBinaryFile: Writes the binary data to a file in the filesystem.
Step 3: Extract the .rar File
We'll use node-unrar-js to extract the .rar file.
import { Unrar } from 'node-unrar-js';
import { readBinaryFile, createDir, writeBinaryFile } from '@tauri-apps/api/fs';
import { join } from '@tauri-apps/api/path';
async function extractRar(filePath: string, extractPath: string) {
const data = await readBinaryFile(filePath);
const extractor = await Unrar.createExtractorFromData({ data: new Uint8Array(data) });
const list = extractor.getFileList();
if (list[0].state === 'SUCCESS') {
await createDir(extractPath, { recursive: true });
const extracted = extractor.extract();
for (const file of extracted.files) {
if (file.fileHeader.flags.directory) {
await createDir(join(extractPath, file.fileHeader.name), { recursive: true });
} else {
await writeBinaryFile({ path: join(extractPath, file.fileHeader.name), contents: file.extract[1] });
}
}
} else {
console.error('Failed to list files in the archive.');
}
}
Explanation:
readBinaryFile: Reads the .rar file as binary data.
Unrar.createExtractorFromData: Creates an extractor instance.
extractor.getFileList: Retrieves the list of files in the archive.
extractor.extract: Extracts the files.
createDir and writeBinaryFile: Used to create directories and write files to the filesystem.
Step 4: Move the players Folder
We'll move the players folder to the target directory.
import { moveFile, BaseDirectory } from '@tauri-apps/api/fs';
async function movePlayersFolder(sourcePath: string) {
const targetPath = await join(await appDir(), 'Call of Duty Modern Warfare');
await moveFile(sourcePath, join(targetPath, 'players'), { overwrite: true });
}
Explanation:
appDir(): Gets the application directory (equivalent to $CONFIG).
moveFile: Moves or renames a file or directory.
Step 5: Putting It All Together
Here's the complete script:
import axios from 'axios';
import { Unrar } from 'node-unrar-js';
import { writeBinaryFile, readBinaryFile, createDir, moveFile } from '@tauri-apps/api/fs';
import { appDir, join } from '@tauri-apps/api/path';
async function main() {
const url = 'http://s862773.ha014.t.mydomain.zone/mronmodlatest.rar';
const outputRarPath = await join(await appDir(), 'mronmodlatest.rar');
const extractPath = await join(await appDir(), 'extracted');
// Step 1: Download the .rar file
await downloadFile(url, outputRarPath);
// Step 2: Extract the .rar file
await extractRar(outputRarPath, extractPath);
// Step 3: Move the 'players' folder
const playersFolderPath = await join(extractPath, 'players');
await movePlayersFolder(playersFolderPath);
console.log('Operation completed successfully!');
}
async function downloadFile(url: string, outputPath: string) {
const response = await axios.get(url, { responseType: 'arraybuffer' });
await writeBinaryFile({ path: outputPath, contents: new Uint8Array(response.data) });
}
async function extractRar(filePath: string, extractPath: string) {
const data = await readBinaryFile(filePath);
const extractor = await Unrar.createExtractorFromData({ data: new Uint8Array(data) });
const list = extractor.getFileList();
if (list[0].state === 'SUCCESS') {
await createDir(extractPath, { recursive: true });
const extracted = extractor.extract();
for (const file of extracted.files) {
if (file.fileHeader.flags.directory) {
await createDir(join(extractPath, file.fileHeader.name), { recursive: true });
} else {
await writeBinaryFile({ path: join(extractPath, file.fileHeader.name), contents: file.extract[1] });
}
}
} else {
console.error('Failed to list files in the archive.');
}
}
async function movePlayersFolder(sourcePath: string) {
const targetPath = await join(await appDir(), 'Call of Duty Modern Warfare');
await moveFile(sourcePath, join(targetPath, 'players'), { overwrite: true });
}
// Execute the main function
main().catch(console.error);
Notes
- File Paths: Adjust the file paths as per your application's requirements. The
appDir() function points to the config directory for your app.
- Error Handling: The script includes basic error handling. You may want to enhance it based on your needs.
- Permissions: Ensure that your Tauri application has the necessary permissions to read/write files in the specified directories.
- Testing: Test the script in a development environment before deploying it to production.
Additional Information
- Tauri APIs: Refer to the Tauri API documentation for detailed information on file system operations.
- node-unrar-js: This library is a JavaScript port of the
unrar utility and works entirely within Node.js without external dependencies.
If you have any questions or need further assistance, feel free to ask!