Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansenn committed Aug 22, 2023
2 parents d019cfc + 509559d commit a0e64a0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 42 deletions.
144 changes: 102 additions & 42 deletions src/main/core/components/Pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,53 @@ import { IpfsHandler } from './ipfsHandler'
import tmp from 'tmp'
import execa from 'execa'
import waIpfs from 'wa-go-ipfs'
import Config from './Config'
import path from 'path'
import fsExtra from 'fs-extra'

const Path = require('path')
const debug = require('debug')('3speak:pins')
const Schedule = require('node-schedule')
PouchDB.plugin(require('pouchdb-find'))
PouchDB.plugin(require('pouchdb-upsert'))
const Utils = require('../utils')

async function progressPin(ipfs: IPFSHTTPClient, pin: string, callback: Function) {

// ipfs.dag.exp
const tmpPath = tmp.dirSync();

const tmpPath = tmp.dirSync()
console.log(tmpPath)
const writer = fs.createWriteStream(Path.join(tmpPath.name, 'download.car'));
const writer = fs.createWriteStream(Path.join(tmpPath.name, 'download.car'))

const {data} = await Axios.get(`https://ipfs-3speak.b-cdn.net/api/v0/object/stat?arg=${pin}`)
const { data } = await Axios.get(`https://ipfs-3speak.b-cdn.net/api/v0/object/stat?arg=${pin}`)
const CumulativeSize = data.CumulativeSize

let totalSizeSoFar = 0;
let totalSizeSoFar = 0
await Axios.get(`https://ipfs-3speak.b-cdn.net/api/v0/dag/export?arg=${pin}`, {
responseType: 'stream',
}).then(response => {


}).then((response) => {
response.data.on('data', (chunk) => {
totalSizeSoFar = totalSizeSoFar + chunk.length
const pct = Math.round((totalSizeSoFar / CumulativeSize) * 100)
callback(pct)
// console.log(`${pct}%`)
})
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
writer.close();
reject(err);
});
response.data.pipe(writer)
let error = null
writer.on('error', (err) => {
error = err
writer.close()
reject(err)
})
writer.on('close', () => {
if (!error) {
resolve(true);
resolve(true)
}
});
});
})
})
})

// console.log('got here')
// for await(let importBlock of ipfs.dag.import(fs.createReadStream(Path.join(tmpPath.name, 'download.car')))) {
// console.log(importBlock)
Expand All @@ -64,21 +66,16 @@ async function progressPin(ipfs: IPFSHTTPClient, pin: string, callback: Function
waIpfs.getDefaultPath({ dev: process.env.NODE_ENV === 'development' }),
)


const output = await execa(ipfsPath, [
'dag',
'import',
Path.join(tmpPath.name, 'download.car')
], {
const output = await execa(ipfsPath, ['dag', 'import', Path.join(tmpPath.name, 'download.car')], {
env: {
IPFS_PATH: ipfsInfo.ipfsPath
}
IPFS_PATH: ipfsInfo.ipfsPath,
},
})
console.log(output)

await fsPromises.rmdir(tmpPath.name, {
recursive: true,
force: true
force: true,
} as any)

// const refs = ipfs.refs(pin, {
Expand Down Expand Up @@ -116,6 +113,75 @@ class Pins {
)
return out
}
async mv(sender) {
try {
const { dialog, BrowserWindow } = require('electron')

const focusedWindow = BrowserWindow.getFocusedWindow()
if (!focusedWindow) {
throw new Error('No focused window found.')
}

// Show a confirmation dialog to the user
const response = await dialog.showMessageBox(focusedWindow, {
type: 'question',
buttons: ['Cancel', 'Proceed'],
title: 'Confirm Folder Move',
message: 'Are you sure you want to move the IPFS folder contents?',
defaultId: 1, // Set the default button (0 for Cancel, 1 for Proceed)
})

// Check user response
if (response.response === 1) {
// Open folder dialog to select new path
const result = await dialog.showOpenDialog(focusedWindow, {
properties: ['openDirectory'],
title: 'Select New IPFS Folder',
message: 'Please select a new folder for IPFS data:',
})

const selectedFolder = result.canceled ? null : result.filePaths[0]

if (selectedFolder) {
// Implement logic to move the contents from the old folder to the new folder
const config = new Config(Utils.getRepoPath())
await config.open()
const ipfsPath = config.get('ipfsPath')

// Normalize paths to ensure consistent comparison
const normalizedIPFSPath = path.normalize(ipfsPath)
const normalizedSelectedFolder = path.normalize(selectedFolder)

// Check if the selected folder is the same as or a parent of the current IPFS folder
if (
normalizedIPFSPath === normalizedSelectedFolder ||
normalizedIPFSPath.startsWith(normalizedSelectedFolder + path.sep)
) {
console.error('Selected folder is the same as or a parent of the current IPFS folder.')
return
}

// Move contents
const files = await fsExtra.readdir(ipfsPath)
for (const file of files) {
const sourcePath = path.join(ipfsPath, file)
const destPath = path.join(selectedFolder, file)
await fsExtra.move(sourcePath, destPath, { overwrite: true })
}

// Update IPFS configuration with the new folder path
config.set('ipfsPath', selectedFolder)

console.log('IPFS folder path changed successfully')
}

// Reply to the sender window with the selected folder path
sender.send('ipfs.handleFolderSelection', selectedFolder)
}
} catch (error) {
console.error('Error handling IPC request:', error)
}
}
async add(doc) {
debug(`received add with id of ${doc._id}`)
if (typeof doc !== 'object') {
Expand All @@ -139,28 +205,22 @@ class Pins {
doc.size = 0
this.inProgressPins[doc._id] = doc


let totalSize = 0
let totalPercent;
let totalPercent

console.log(doc)

const progressTick = setInterval(async() => {

const progressTick = setInterval(async () => {
try {
const bDoc = await this.db.get("hive:cowboyzlegend27:qauvdrmx")
const bDoc = await this.db.get('hive:cowboyzlegend27:qauvdrmx')
console.log(bDoc)
} catch {

}
} catch {}
try {
const cDoc = await this.db.get(doc._id)
console.log(cDoc)
} catch {

}
} catch {}
await this.db.upsert(doc._id, (oldDoc) => {
oldDoc.percent = totalPercent;
oldDoc.percent = totalPercent
doc.percent = totalPercent
return oldDoc
})
Expand Down
3 changes: 3 additions & 0 deletions src/main/ipcAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class IpcAdapter {
PromiseIPC.on('pins.ls', async () => {
return await this.core.pins.ls()
})
PromiseIPC.on('pins.mv', async (ref) => {
return await this.core.pins.mv(ref.sender)
})
//Accounts
PromiseIPC.on('accounts.createProfile', async (doc) => {
return await this.core.accounts.createProfile(doc)
Expand Down
1 change: 1 addition & 0 deletions src/renderer/views/PinsView/PinsViewComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const PinsViewComponent: React.FC<PinsViewProps> = ({
<Dropdown.Item eventKey="1">Manual Pin</Dropdown.Item>
<Dropdown.Item eventKey="2">Manual GC</Dropdown.Item>
<Dropdown.Item eventKey="3">IPFS Folder</Dropdown.Item>
<Dropdown.Item eventKey="4">Change path</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</Col>
Expand Down
11 changes: 11 additions & 0 deletions src/renderer/views/PinsView/pinningUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ export const usePinningUtils = () => {
shell.openPath(ipfsPath)
}

const handleCase4 = async () => {
try {
await PromiseIpc.send('pins.mv')
} catch (error) {
console.error('Error sending IPC message:', error)
}
}

const actionSelect = async (key) => {
switch (key) {
case '1':
Expand All @@ -184,6 +192,9 @@ export const usePinningUtils = () => {
case '3':
await handleCase3()
break
case '4':
await handleCase4()
break
default:
}
}
Expand Down

0 comments on commit a0e64a0

Please sign in to comment.