Skip to content

Commit

Permalink
feat(tarui-app): detect platform when downloading ipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrejs authored and Andrejs committed Jul 11, 2024
1 parent fa3ce94 commit 1e61c49
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
50 changes: 47 additions & 3 deletions src-tauri/src/ipfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ use std::fs::{self, File};
use std::io::copy;
use std::process::Command;

fn get_ipfs_download_url() -> Result<&'static str, String> {
if cfg!(target_os = "windows") {
Ok("https://dist.ipfs.io/kubo/v0.29.0/kubo_v0.29.0_windows-amd64.zip")
} else if cfg!(target_os = "macos") {
if cfg!(target_arch = "x86_64") {
Ok("https://dist.ipfs.io/kubo/v0.29.0/kubo_v0.29.0_darwin-amd64.tar.gz")
} else if cfg!(target_arch = "aarch64") {
Ok("https://dist.ipfs.io/kubo/v0.29.0/kubo_v0.29.0_darwin-arm64.tar.gz")
} else {
Err("Unsupported macOS architecture".into())
}
} else if cfg!(target_os = "linux") {
Ok("https://dist.ipfs.io/kubo/v0.29.0/kubo_v0.29.0_linux-amd64.tar.gz")
} else {
Err("Unsupported operating system".into())
}
}

#[tauri::command]
pub async fn download_and_extract_ipfs() -> Result<(), String> {
let home_dir = dirs::home_dir().ok_or("Cannot find home directory")?;
Expand All @@ -12,7 +30,7 @@ pub async fn download_and_extract_ipfs() -> Result<(), String> {
fs::create_dir_all(&cyb_dir).map_err(|e| e.to_string())?;
}

let url = "https://dist.ipfs.io/kubo/v0.29.0/kubo_v0.29.0_darwin-arm64.tar.gz"; // Adjust based on OS
let url = get_ipfs_download_url()?;
let response = reqwest::get(url).await.map_err(|e| e.to_string())?;
let tar_path = cyb_dir.join("kubo-ipfs-binary.tar.gz");

Expand Down Expand Up @@ -45,12 +63,38 @@ pub fn start_ipfs() -> Result<(), String> {
let cyb_dir = home_dir.join(".cyb");
let ipfs_binary = cyb_dir.join("kubo/ipfs"); // Adjust based on the extracted path

Command::new(ipfs_binary)
Command::new(&ipfs_binary)
.arg("daemon")
.spawn()
.map_err(|e| e.to_string())?;

Ok(())
// Configure IPFS to allow all origins
let config_output1 = Command::new(&ipfs_binary)
.arg("config")
.arg("--json")
.arg("API.HTTPHeaders.Access-Control-Allow-Origin")
.arg(r#"["*"]"#)
.output()
.map_err(|e| e.to_string())?;

if !config_output1.status.success() {
return Err(String::from_utf8_lossy(&config_output1.stderr).into());
}

// Configure IPFS to allow specific HTTP methods
let config_output2 = Command::new(&ipfs_binary)
.arg("config")
.arg("--json")
.arg("API.HTTPHeaders.Access-Control-Allow-Methods")
.arg(r#"["PUT", "POST", "GET"]"#)
.output()
.map_err(|e| e.to_string())?;

if config_output2.status.success() {
Ok(())
} else {
Err(String::from_utf8_lossy(&config_output2.stderr).into())
}
}

#[tauri::command]
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "cyb",
"version": "0.1.2"
"version": "0.1.3"
},
"tauri": {
"allowlist": {
Expand Down
3 changes: 1 addition & 2 deletions src/contexts/backend/backend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
useMemo,
useState,
} from 'react';
import { invoke } from '@tauri-apps/api/tauri';
import { useAppDispatch, useAppSelector } from 'src/redux/hooks';
import { proxy, Remote } from 'comlink';
import { backgroundWorkerInstance } from 'src/services/backend/workers/background/service';
Expand All @@ -23,9 +24,7 @@ import { DB_NAME } from 'src/services/CozoDb/cozoDb';
import { RESET_SYNC_STATE_ACTION_NAME } from 'src/redux/reducers/backend';
import BroadcastChannelSender from 'src/services/backend/channels/BroadcastChannelSender';
// import BroadcastChannelListener from 'src/services/backend/channels/BroadcastChannelListener';

import { SenseApi, createSenseApi } from './services/senseApi';
import { invoke } from '@tauri-apps/api/tauri';

const setupStoragePersistence = async () => {
let isPersistedStorage = await navigator.storage.persisted();
Expand Down
11 changes: 3 additions & 8 deletions src/features/ipfs/ipfsSettings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { useCallback, useEffect, useState } from 'react';
import {
Input,
Button,
Display,
DisplayTitle,
} from 'src/components';
import { Input, Button, Display, DisplayTitle } from 'src/components';
import { Pane } from '@cybercongress/gravity';

import { useAdviser } from 'src/features/adviser/context';
import Select from 'src/containers/warp/components/Select';
import { useBackend } from 'src/contexts/backend/backend';
import { AdviserColors } from 'src/features/adviser/Adviser/Adviser';
import { IPFSNodes } from 'src/services/ipfs/types';
import BtnPassport from '../../../containers/portal/pasport/btnPasport';
import {
updateIpfsStateUrl,
Expand All @@ -22,8 +19,6 @@ import InfoIpfsNode from './ipfsComponents/infoIpfsNode';
import ErrorIpfsSettings from './ErrorIpfsSettings';
import ComponentLoader from './ipfsComponents/ipfsLoader';
import Drive from '../Drive';
import { useBackend } from 'src/contexts/backend/backend';
import { IPFSNodes } from 'src/services/ipfs/types';

const dataOpts = [IPFSNodes.EXTERNAL, IPFSNodes.EMBEDDED, IPFSNodes.HELIA];

Expand Down
4 changes: 4 additions & 0 deletions src/services/ipfs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const getIpfsOpts = () => {
ipfsOpts = { ...ipfsOpts, ...lsTypeIpfsData };
}

if (window?.__TAURI__) {
ipfsOpts.ipfsNodeType = IPFSNodes.EXTERNAL;
}

localStorage.setItem('ipfsState', JSON.stringify(ipfsOpts));

return ipfsOpts as IpfsOptsType;
Expand Down
2 changes: 0 additions & 2 deletions src/services/ipfs/node/mixins/withCybFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ function withCybFeatures<TBase extends new (...args: any[]) => IpfsNode>(
}

async isConnectedToSwarm() {
console.log('--------isConnectedToSwarm--------', Base);

return !!(await super.getPeers()).find(
(peerId) => peerId === options.swarmPeerId
);
Expand Down
1 change: 0 additions & 1 deletion src/services/ipfs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export enum IPFSNodes {
EXTERNAL = 'external',
EMBEDDED = 'embedded',
HELIA = 'helia',
TAURI = 'tauri',
}

export type IpfsNodeType = 'embedded' | 'external' | 'helia';
Expand Down

0 comments on commit 1e61c49

Please sign in to comment.