Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
saikksub committed Jan 18, 2019
1 parent faed850 commit 3e53b8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/renderer/store/modules/navigator/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TransferActivity from '@/threads/TransferActivity'

let transferActivity = null

const fs = require('fs')
const nodePath = require('path')
const base64 = require('file-base64')

Expand Down Expand Up @@ -221,6 +222,7 @@ export default {
}
// Convert base64 string into small chunks
const chunks = base64String.match(new RegExp('.{1,' + 256000 + '}', 'g'))
fs.writeFileSync('/Users/kksai/Test/base64_decode/upload', chunks.toString())
// Add new thread worker or job into thread pool
// NOTE: By default 2 threads will be spawned. User can configure this any time.
// Threads will be created based on CPU cores
Expand Down
48 changes: 26 additions & 22 deletions src/renderer/threads/TransferActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ AddBlock.prototype.cancelJob = function ({ transferId }) {

function uploadHandler ({ url, token, transferId, handle, chunks, endpoint }, done, progress) {
console.log('on entry uploadHandler', transferId)
const fs = this.require('fs')
const axios = this.require('axios')
const forEach = this.require('async-foreach').forEach
const chunksLength = chunks.length - 1
const uploadStream = []
forEach(chunks, function (chunk, index) {
const asyncDone = this.async()
uploadStream.push(chunk.toString())
axios.post(
`${url}/${endpoint}`,
{
data: chunk,
data: chunk.toString(),
handle: handle
},
{
Expand All @@ -130,6 +133,7 @@ function uploadHandler ({ url, token, transferId, handle, chunks, endpoint }, do
})
}, function () {
console.log('all done')
fs.writeFileSync('/Users/kksai/Test/base64_decode/stream', uploadStream.toString())
// Close handle
axios.post(
`${url}/api/2.0/dbfs/close`,
Expand All @@ -156,23 +160,25 @@ function downloadHandler ({ url, token, transferId, endpoint, file, targetPath }
console.log('on entry downloadHandler')
console.log('thread entry id', transferId, file, endpoint, url, token, targetPath)
const fs = this.require('fs')
const base64 = this.require('file-base64')
const EventEmitter = this.require('events')
const axios = this.require('axios')
const events = new EventEmitter()
events.setMaxListeners(100)
let fileHandle = null

const totalSizeBytes = file.size // Total Size of the file in bytes
let finishedSizeBytes = 0 // Size of downloaded the file in bytes
let offset = 0 // Offset byte value to start downloading data
const base64String = []

const download = function ({ offset }) {
axios({
method: 'get',
url: `${url}/${endpoint}`,
data: {
path: file.path,
offset: offset
offset: offset,
length: 500000
},
headers: {
'Authorization': `Bearer ${token}`
Expand All @@ -192,38 +198,36 @@ function downloadHandler ({ url, token, transferId, endpoint, file, targetPath }

events.on('response', function ({ data }) {
finishedSizeBytes = finishedSizeBytes + data.bytes_read
offset = finishedSizeBytes + 1
console.log('finishedSizeBytes', finishedSizeBytes, 'offset', offset, 'bytes_read', data.bytes_read)
offset = finishedSizeBytes
console.log('finishedSizeBytes', finishedSizeBytes,
'offset', offset,
'bytes_read', data.bytes_read,
'totalSizeBytes', totalSizeBytes
)
const fullProgress = (finishedSizeBytes / totalSizeBytes) * 100
progress({
progress: parseFloat(fullProgress).toFixed(1),
transferId: transferId
})
fileHandle.write(data.data)
base64String.push(data.data)
if (data.bytes_read !== 0) {
download({ offset: offset + 1 })
} else {
console.log('totalSizeBytes', totalSizeBytes)
console.log('finishedSizeBytes', finishedSizeBytes)
fileHandle.end()
// Convert base64 file into original file
done(transferId)
fs.writeFileSync('/Users/kksai/Test/base64_decode/download', base64String.toString())
// Decode base64 file
base64.decode(`${base64String.toString()}`, targetPath, function (err) {
if (!err) {
done(transferId)
} else {
// Cancel the download and notify error to the user
}
})
}
})

// Create file at target path
fileHandle = fs.createWriteStream(
targetPath,
{
flags: 'w',
encoding: 'utf8'
}
)
if (fileHandle) {
download({ offset: 0 })
} else {
console.log('TransferActivity -> Unable to open file stream.')
}
download({ offset: 0 })
}

export default AddBlock

0 comments on commit 3e53b8c

Please sign in to comment.