Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// build a path to an unused name in the downloads folder
let filePath = opts.saveAs ? opts.saveAs : unusedFilename.sync(path.join(app.getPath('downloads'), item.getFilename()))
// track as an active download
item.id = ('' + Date.now()) + ('' + Math.random()) // pretty sure this is collision proof but replace if not -prf
item.name = path.basename(filePath)
if (item.name.split('.').length < 2 && item.getMimeType()) {
const ext = `.${mime.extension(item.getMimeType())}`
if (ext !== '.bin') {
item.name += ext
filePath += ext
}
}
item.setSavePath(filePath)
item.isHandled = true
item.downloadSpeed = speedometer()
if (!opts.trusted) {
item.pause()
var allowed = await requestPermission('download', wc, {url: item.getURL(), filename: item.name})
if (!allowed) {
item.cancel()
return
}
item.resume()
}
downloads.push(item)
downloadsEvents.emit('new-download', toJSON(item))
// update dock-icon progress bar
var lastBytes = 0
const [promise, done] = prom()
const updateState = newState => this.setState(state => (
{ files: updateAt(state.files, i, newState) }
))
updateState({ pending: true })
let { path, api, archive } = this.props
const key = archive
let { name, webkitRelativePath } = file
if (this.state.uploadDir) name = webkitRelativePath
path = (path === '/' ? '' : path) + '/' + name
const speedo = speedometer()
let speed = 0
let written = 0
// Set up a passthrough stream to track stats.
const passthrough = through(function (chunk, enc, next) {
written += chunk.length
speed = speedo(chunk.length)
this.push(chunk)
next()
})
// The actual streaming file reader.
const reader = fileReader(file)
// The write stream to the backend.
const ws = await api.hyperdrive.createWriteStream(key, path)
async uploadFile (file, i) {
this.setState({ files: updateAt(this.state.files, i, { pending: true }) })
const { dir, core } = this.props
const { name } = file
const path = (dir === '/' ? '' : dir) + '/' + name
const speedo = speedometer()
let speed = 0
let written = 0
const update = () => {
// todo: rerender only if state acually changed.
this.setState({ files: updateAt(this.state.files, i, { written, speed }) })
}
let debounce = setInterval(update, 200)
const passthrough = through((chunk, enc, next) => {
written += chunk.length
speed = speedo(chunk.length)
next()
})
const reader = fileReader(file)
pump(reader, passthrough)
const key = this.props.archive
async uploadFile (file, i) {
this.setState({ files: updateAt(this.state.files, i, { pending: true }) })
const { dir, core } = this.props
let { name, webkitRelativePath } = file
if (this.state.uploadDir) name = webkitRelativePath
const path = (dir === '/' ? '' : dir) + '/' + name
const speedo = speedometer()
let speed = 0
let written = 0
const update = () => {
// todo: rerender only if state acually changed.
this.setState({ files: updateAt(this.state.files, i, { written, speed }) })
}
let debounce = setInterval(update, 200)
const passthrough = through((chunk, enc, next) => {
written += chunk.length
speed = speedo(chunk.length)
next()
})
const reader = fileReader(file)
pump(reader, passthrough)
const key = this.props.archive
setupSpeedometer() {
this.speed = speedometer();
}
export default function Stats (emitter, { archiveInfo, onToggleServing }) {
if (!(this instanceof Stats)) return new Stats(emitter)
this.emitter = emitter
this.archiveInfo = archiveInfo
this.uploadSpeed = speedometer()
this.downloadSpeed = speedometer()
this.onToggleServing = onToggleServing
this.onUpdatePeers = ({ key, peers }) => {
if (key === archiveInfo.key) {
this.archiveInfo.peers = peers
this.updateActives()
}
}
this.onDownload = ({ key, index, bytes }) => {
if (key === archiveInfo.key) {
this.downloadSpeed(bytes)
this.updateActives()
}
}
this.onUpload = ({ key, index, bytes }) => {