Constructor
 Download a file or resource
 @param options [Hash]
 @param target [String, nil] system path to the downloaded file (defaults to a temporary file)
434203
def download(spec, target = nil)
      if target.nil?
        ext = File.extname(spec['file_name'])
        base = File.basename(spec['file_name'], ext)
        target = Dir::Tmpname.create([base, ext]) {}
      end

      File.open(target, 'wb') do |output|
        retrieve(spec) do |chunk, retrieved, total|
          output.write(chunk)
          yield(target, retrieved, total) if block_given?
        end
      end
      target
    end
452840
function cheetahDownloader (FILEURL, TARGETFOLDER, OPTS = {}) {
  if (!TARGETFOLDER) {
    throw Error('Cheetah: TARGETFOLDER is required as second parameter.')
  } else if (typeof TARGETFOLDER !== 'string') {
    throw Error('Cheetah: Second parameter should be string.')
  }

  const fs = require('fs')
  const URL = require('url')
  const path = require('path')
  const rimraf = require('rimraf')
  const mkdirp = require('mkdirp')
  const request = require('request')
  const progress = require('request-progress')

  // Make sure the target folder exists
  fs.accessSync(TARGETFOLDER, fs.W_OK)  // Node will throw an exception if path does not exists

  const THREADS = OPTS.connections || 5
  const DIR_TEMP = OPTS.tempDir || `${__dirname}/temp/`  // for all the downloads
  const UID = Math.random().toString(36).substr(2, 4)   // 4 letter random aplha-numeric string
  const PATH_TEMP = `${DIR_TEMP}${UID}/`  // for current download
  const INTERVAL = OPTS.interval || 500
  const UA = OPTS.UA || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'

  // Make sure the temp folder is writeable
  fs.accessSync(DIR_TEMP, fs.W_OK)  // Node will throw an exception if path does not exists

  const STATUS = {
    init: 0,
    meta: 1,
    downloading: 2,
    paused: 3,
    combining: 4,
    ended: 5
  }

  let targetPath // final path with file name
  const instance = {}
  const eventStore = {
    meta: [],
    error: [],
    progress: [],
    complete: [],
    chunkProgress: []
  }
  const rangeStore = [] // store data about each chunk being downloaded
  const currentFile = {
    status: STATUS.init
  }

  const trigger = (evt, data) => {
    eventStore[evt].forEach(callback => callback(data))
  }

  const getFilename = type => {
    const parsed = URL.parse(FILEURL)
    return path.basename(parsed.pathname)
  }

  const applyToRequest = action => {
    rangeStore.forEach(req => {
      if (action === 'pause' || action === 'abort') {
        req.request.abort()
      } else { // resume
        sendRangeRequest(FILEURL, {
          to: req.state.size.total,
          from: req.state.size.transferred
        }, req.index)
      }
    })
  }

  const markEnded = (cb = () => {}) => {
    // cleanup temp directory.
    rimraf(PATH_TEMP, cb)
    currentFile.status = STATUS.ended
  }

  const markComplete = () => {
    trigger('complete', {
      path: targetPath
    })
    markEnded()
  }

  const appendChunk = (flag = 'r+') => {
    if (rangeStore.length) {
      const chunk = rangeStore.pop()
      const i = chunk.index

      fs.createReadStream(PATH_TEMP + 'chunk-' + i)
      .on('error', error => {
        trigger('error', {
          error: error,
          code: 'CHUNK_FAIL',
          msg: 'Could not join the downloaded parts together.'
        })
        markEnded()
      })
      .on('end', appendChunk)
      .pipe(fs.createWriteStream(targetPath, {
        flags: flag,
        start: chunk.range.from
      }))
    } else { // all ended
      markComplete()
    }
  }

  const putChunksTogether = () => { // joins all the chunks into final file
    fs.access(targetPath, err => {
      appendChunk(err ? 'w' : 'r+')
    })
  }

  const chunkComplete = () => {
    if (currentFile.status >= STATUS.combining) {
      return
    }

    let completed = 0

    for (let i = 0; i < rangeStore.length; i++) {
      if (rangeStore[i].complete) {
        completed++
      }
    }

    if (completed === rangeStore.length) { // all parts are done
      putChunksTogether()
      currentFile.status = STATUS.combining
    }
  }

  const progessInterval = () => {
    if (currentFile.status !== STATUS.downloading) {
      return
    }

    let state
    let dSize = 0
    let dTime = 0
    let dSpeed = 0
    const n = rangeStore.length

    for (var i = 0; i < n; i++) {
      state = rangeStore[i].state
      if (state) { // could be undefined initially
        dSpeed += state.speed
        dTime += state.time.remaining
        dSize += state.size.transferred
      }
    }

    trigger('progress', {
      speed: (dSpeed / 1024).toFixed(2), // kb/s
      timeRemaining: (dTime / THREADS).toFixed(2), // sec
      percent: ((dSize / currentFile.size) * 100).toFixed(3)
    })

    setTimeout(progessInterval, INTERVAL)
  }

  const reportChunkProgress = (index, progress) => {
    trigger('chunkProgress', {
      index: index,
      speed: (progress.speed / 1024).toFixed(2), // kb/s
      percent: (progress.percent || 0).toFixed(3),
      timeRemaining: (progress.time.remaining || 0).toFixed(2) // sec
    })
  }

  const getRange = (number, n = 5) => {
    let temp
    let lastByte = 0
    const ranges = []
    const f1 = number / n

    for (let i = 1; i <= n; i++) {
      temp = f1 * i
      ranges.push({
        to: Math.floor(temp),
        from: Math.ceil(lastByte)
      })
      lastByte = temp
    }

    return ranges
  }

  const sendRangeRequest = (url, range, index) => {
    const req = request.get(url, {
      gzip: true,
      encoding: null,
      followRedirect: true,
      headers: {
        'User-Agent': UA,
        Range: `bytes=${range.from}-${range.to}`
      }
    })

    rangeStore[index] = rangeStore[index] || {}
    rangeStore[index].range = range
    rangeStore[index].index = index
    rangeStore[index].request = req

    const prog = progress(req)
    .on('error', function (err) {
      trigger('error', {
        error: err,
        code: 'DOWNLOAD_FAIL',
        msg: 'Some error occurred while downloading a part of file.'
      })
      markEnded()
    })
    .on('progress', function (state) {
      rangeStore[index].state = state
      reportChunkProgress(index, state)
    })
    .on('end', function () {
      if (currentFile.status !== STATUS.paused) {
        rangeStore[index].complete = true
        chunkComplete()
      }
    })
    fs.access(PATH_TEMP + 'chunk-' + index, function (err) {
      prog.pipe(fs.createWriteStream(PATH_TEMP + 'chunk-' + index, {
        flags: err ? 'w' : 'r+'
      }))
    })
  }

  const handleError = () => {}

  const sendHeadRequest = (url, cb, errCB) => {
    const reqOpts = {
      url: url,
      timeout: 10000,
      strictSSL: false,
      followRedirect: true,
      headers: {
        'User-Agent': UA
      }
    }

    request.head(reqOpts)
    .on('response', cb)
    .on('error', function (err, res) {
      handleError(err) // eslint fix
      if (res.code === 405) { // Method not allowed = HEAD
        const getReq = request.get(reqOpts) // Try GETting it
        .on('response', res => {
          cb(res)
          getReq.abort() // just needed the headers
        })
        .on('error', errCB)
      }
    })
  }

  const getMeta = () => {
    // check to see if file exists, whats its size and does it accepts ranges.
    sendHeadRequest(FILEURL, function (res) {
      if (res.statusCode === 200) {
        const type = res.headers['content-type']
        const range = res.headers['accept-ranges']
        const size = res.headers['content-length']

        const resumable = !!(range && range !== 'none')

        currentFile.size = size
        currentFile.status = STATUS.meta
        currentFile.resumable = resumable

        const filename = getFilename(type)
        targetPath = path.join(TARGETFOLDER, OPTS.saveAs || filename)

        trigger('meta', {
          size: size,
          type: type,
          saveAs: filename,
          resumable: resumable
        })
      } else if (res.statusCode >= 400) {
        trigger('error', {
          error: res,
          code: 'OUT_OF_REACH',
          msg: 'Some error occurred in accessing the file.'
        })
      }
    }, function (err) {
      trigger('error', {
        error: err,
        code: 'OUT_OF_REACH',
        msg: 'Some error occurred in accessing the file.'
      })
    })
  }

  instance.start = () => {
    if (currentFile.status < STATUS.meta) {
      prepare()
      return instance.on('meta', instance.start) // get a callback when meta is available.
    }

    if (currentFile.resumable) {
      getRange(currentFile.size, THREADS).forEach(function (r, i) {
        sendRangeRequest(FILEURL, r, i)
      })
    } else {
      sendRangeRequest(FILEURL, {
        from: 0,
        to: currentFile.size
      }, 0)
    }

    currentFile.status = STATUS.downloading
    progessInterval() // start reporting
    return instance
  }

  instance.pause = () => {
    currentFile.status = STATUS.paused
    applyToRequest('pause')
    return instance
  }

  instance.resume = () => {
    if (currentFile.status === STATUS.paused) {
      currentFile.status = STATUS.downloading
      progessInterval()
      applyToRequest('resume')
    }
    return instance
  }

  instance.abort = () => {
    applyToRequest('abort')
    markEnded()
    return instance
  }

  process.on('SIGINT', function () { // cleanup on CTRL+C
    markEnded(process.exit)
  })

  instance.on = (evt, evtCB) => {
    if (eventStore[evt] && evtCB) {
      eventStore[evt].push(evtCB)
    }

    return instance
  }

  instance.getMeta = getMeta

  const prepare = () => {
    getMeta()
    mkdirp(PATH_TEMP)
  }

  return instance
}
584296
def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, **options, &block)
      response = request(url, **options, &block)

      content_length_proc.call(response.content_length) if content_length_proc && response.content_length

      if max_size && response.content_length && response.content_length > max_size
        raise Down::TooLarge, "file is too large (max is #{max_size/1024/1024}MB)"
      end

      extname  = File.extname(response.uri.path)
      tempfile = Tempfile.new(["down-http", extname], binmode: true)

      stream_body(response) do |chunk|
        tempfile.write(chunk)
        chunk.clear # deallocate string

        progress_proc.call(tempfile.size) if progress_proc

        if max_size && tempfile.size > max_size
          raise Down::TooLarge, "file is too large (max is #{max_size/1024/1024}MB)"
        end
      end

      tempfile.open # flush written content

      tempfile.extend Down::Http::DownloadedFile
      tempfile.url     = response.uri.to_s
      tempfile.headers = response.headers.to_h

      download_result(tempfile, destination)
    rescue
      tempfile.close! if tempfile
      raise
    end
1172293
def download_file(response)
      tmp_file = Tempfile.new '', Configuration.temp_folder_path
      content_disposition = response.headers['Content-Disposition']
      if content_disposition
        filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
        path = File.join File.dirname(tmp_file), filename
      else
        path = tmp_file.path
      end
      # close and delete temp file
      tmp_file.close!

      File.open(path, 'w') { |file| file.write(response.body) }
      Configuration.logger.info "File written to #{path}. Please move the file to a proper "\
                                "folder for further processing and delete the temp afterwards"
      File.new(path)
    end
535230
function(successCallback, errorCallback, options) {
        var source = options[0];
        var target = options[1];
        var downloadId = options[3];
        var headers = options[4] || {};

        if (!target) {
            errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
            return;
        }
        if (target.substr(0, 8) === "file:///") {
            target = appData.localFolder.path + target.substr(8).split("/").join("\\");
        } else if (target.indexOf('ms-appdata:///') === 0) {
            // Handle 'ms-appdata' scheme
            target = target.replace('ms-appdata:///local', appData.localFolder.path)
                           .replace('ms-appdata:///temp', appData.temporaryFolder.path);
        } else if (target.indexOf('cdvfile://') === 0) {
            target = target.replace('cdvfile://localhost/persistent', appData.localFolder.path)
                           .replace('cdvfile://localhost/temporary', appData.temporaryFolder.path);
        }
        target = cordovaPathToNative(target);

        var path = target.substr(0, target.lastIndexOf("\\"));
        var fileName = target.substr(target.lastIndexOf("\\") + 1);
        if (path === null || fileName === null) {
            errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
            return;
        }
        // Download to a temp file to avoid the file deletion on 304 
        // CB-7006 Empty file is created on file transfer if server response is 304
        var tempFileName = '~' + fileName;

        var download = null;

        // Create internal download operation object
        fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);

        var downloadCallback = function(storageFolder) {
            storageFolder.createFileAsync(tempFileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (storageFile) {

                if (alreadyCancelled(downloadId)) {
                    errorCallback(new FTErr(FTErr.ABORT_ERR, source, target));
                    return;
                }

                // if download isn't cancelled, contunue with creating and preparing download operation
                var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
                for (var header in headers) {
                    if (headers.hasOwnProperty(header)) {
                        downloader.setRequestHeader(header, headers[header]);
                    }
                }

                // create download object. This will throw an exception if URL is malformed
                try {
                    var uri = Windows.Foundation.Uri(source);
                    download = downloader.createDownload(uri, storageFile);
                } catch (e) {
                    // so we handle this and call errorCallback
                    errorCallback(new FTErr(FTErr.INVALID_URL_ERR));
                    return;
                }

                var downloadOperation = download.startAsync();
                // update internal TransferOperation object with newly created promise
                fileTransferOps[downloadId].promise = downloadOperation;

                downloadOperation.then(function () {

                    // Update TransferOperation object with new state, delete promise property
                    // since it is not actual anymore
                    var currentDownloadOp = fileTransferOps[downloadId];
                    if (currentDownloadOp) {
                        currentDownloadOp.state = FileTransferOperation.DONE;
                        currentDownloadOp.promise = null;
                    }

                    storageFile.renameAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).done(function () {
                        var nativeURI = storageFile.path.replace(appData.localFolder.path, 'ms-appdata:///local')
                        .replace(appData.temporaryFolder.path, 'ms-appdata:///temp')
                        .replace(/\\/g, '/');

                        // Passing null as error callback here because downloaded file should exist in any case
                        // otherwise the error callback will be hit during file creation in another place
                        FileProxy.resolveLocalFileSystemURI(successCallback, null, [nativeURI]);
                    }, function(error) {
                        errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
                    });
                }, function(error) {

                    var getTransferError = new WinJS.Promise(function (resolve) {
                        // Handle download error here. If download was cancelled,
                        // message property will be specified
                        if (error.message === 'Canceled') {
                            resolve(new FTErr(FTErr.ABORT_ERR, source, target, null, null, error));
                        } else if (error && error.number === HTTP_E_STATUS_NOT_MODIFIED) {
                            resolve(new FTErr(FTErr.NOT_MODIFIED_ERR, source, target, 304, null, error));
                        } else {
                            // in the other way, try to get response property
                            var response = download.getResponseInformation();
                            if (!response) {
                                resolve(new FTErr(FTErr.CONNECTION_ERR, source, target));
                            } else {
                                if (download.progress.bytesReceived === 0) {
                                    resolve(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, null, error));
                                    return;
                                }
                                var reader = new Windows.Storage.Streams.DataReader(download.getResultStreamAt(0));
                                reader.loadAsync(download.progress.bytesReceived).then(function (bytesLoaded) {
                                    var payload = reader.readString(bytesLoaded);
                                    resolve(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, payload, error));
                                });
                            }
                        }
                    });
                    getTransferError.then(function (fileTransferError) {

                        // Update TransferOperation object with new state, delete promise property
                        // since it is not actual anymore
                        var currentDownloadOp = fileTransferOps[downloadId];
                        if (currentDownloadOp) {
                            currentDownloadOp.state = FileTransferOperation.CANCELLED;
                            currentDownloadOp.promise = null;
                        }

                        // Cleanup, remove incompleted file
                        storageFile.deleteAsync().then(function() {
                            errorCallback(fileTransferError);
                        });
                    });

                }, function(evt) {

                    var progressEvent = new ProgressEvent('progress', {
                        loaded: evt.progress.bytesReceived,
                        total: evt.progress.totalBytesToReceive,
                        target: evt.resultFile
                    });
                    // when bytesReceived == 0, BackgroundDownloader has not yet differentiated whether it could get file length or not,
                    // when totalBytesToReceive == 0, BackgroundDownloader is unable to get file length
                    progressEvent.lengthComputable = (evt.progress.bytesReceived > 0) && (evt.progress.totalBytesToReceive > 0);

                    successCallback(progressEvent, { keepCallback: true });
                });
            }, function(error) {
                errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
            });
        };

        var fileNotFoundErrorCallback = function(error) {
            errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
        };

        Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback, function (error) {
            // Handle non-existent directory
            if (error.number === -2147024894) {
                var parent = path.substr(0, path.lastIndexOf('\\')),
                    folderNameToCreate = path.substr(path.lastIndexOf('\\') + 1);

                Windows.Storage.StorageFolder.getFolderFromPathAsync(parent).then(function(parentFolder) {
                    parentFolder.createFolderAsync(folderNameToCreate).then(downloadCallback, fileNotFoundErrorCallback);
                }, fileNotFoundErrorCallback);
            } else {
                fileNotFoundErrorCallback();
            }
        });
    }
328437
def fetch(
            self,
            url,
            filename=None,
            decompress=False,
            force=False,
            timeout=None,
            use_wget_if_available=True):
        
        key = (url, decompress)
        if not force and key in self._local_paths:
            path = self._local_paths[key]
            if exists(path):
                return path
            else:
                del self._local_paths[key]
        path = download.fetch_file(
            url,
            filename=filename,
            decompress=decompress,
            subdir=self.subdir,
            force=force,
            timeout=timeout,
            use_wget_if_available=use_wget_if_available)

        self._local_paths[key] = path
        return path
744968
public static void copyResourceFile(String source, Path target) throws IOException {
        InputStream resource = FsCrawlerUtil.class.getResourceAsStream(source);
        FileUtils.copyInputStreamToFile(resource, target.toFile());
    }
1096150
def file_download(object_id, input_params={}, always_retry=True, **kwargs):
    
    return DXHTTPRequest('/%s/download' % object_id, input_params, always_retry=always_retry, **kwargs)
926474
def choice_download(self):
        
        Download(path="", url=self.dwn_srcs, repo="sbo").start()
        raise SystemExit()
832677
def download_result(tempfile, destination)
      return tempfile unless destination

      tempfile.close # required for Windows
      FileUtils.mv tempfile.path, destination

      nil
    end
501949
def download(path, default_ext=None):
    
    key = "DOWNLOAD:{}".format(path)
    if not config.UPDATE and FILECACHE.get(key):
        return FILECACHE.get(key).decode('utf-8')

    config.LOGGER.info("\tDownloading {}".format(path))

    # Write file to temporary file
    with tempfile.TemporaryFile() as tempf:
        hash = write_and_get_hash(path, tempf)
        tempf.seek(0)

        # Get extension of file or default if none found
        extension = os.path.splitext(path)[1][1:].lower()
        if extension not in [key for key, value in file_formats.choices]:
            if default_ext:
                extension = default_ext
            else:
                raise IOError("No extension found: {}".format(path))

        filename = '{0}.{ext}'.format(hash.hexdigest(), ext=extension)

        copy_file_to_storage(filename, tempf)

        FILECACHE.set(key, bytes(filename, "utf-8"))

        return filename
127391
def download_file(self, file_url, destination_folder=None):
        
        if not destination_folder:
            destination_folder = constants.Files.DOWNLOADS_FOLDER
        page_utils._download_file_to(file_url, destination_folder)
402631
private function downloadTemporaryFile($url, $target)
    {
        try {
            if (($fh = @fopen($target, 'w')) === false) {
                throw new RuntimeException(
                    sprintf(
                        'Could not create file "%s": %s',
                        $target,
                        @error_get_last()['message']
                    )
                );
            }

            if ((@fwrite($fh, $this->httpClient->get($url, 'application/octet-stream'))) === false) {
                throw new RuntimeException(
                    sprintf(
                        'The "%s" file could not be downloaded: %s',
                        $url,
                        @error_get_last()['message']
                    )
                );
            }
        } finally {
            if (is_resource($fh)) {
                fclose($fh);
            }
        }
    }
412064
public void downloadFile(int fileId, java.io.File target, FileSize size)
			throws IOException {
		WebResource builder = getResourceFactory()
				.getFileResource("/" + fileId);
		if (size != null) {
			builder = builder.path("/" + size.name().toLowerCase());
		}
		byte[] data = builder.get(byte[].class);
		FileUtils.writeByteArrayToFile(target, data);
	}
16661
def reload_target!(options = {})
      old_options = @target_load_options || {}
      options = (@target_load_options || {}).merge(options)
      if options[:skip_first_time] && @target_load_count == 1
        @target_load_count += 1
      else
        load_target!(options.merge(reload: true))
      end
    end
684466
def http_post_download(url, data, dest)
    File.open(dest, "w") do |io|
      req = HTTPI::Request.new(:url => url, :body => data)
      req.on_body { |d| io << d; d.length }
      HTTPI.post(req)
    end
    true
  end
1273998
def download_from(self, buff, remote_path):
        
        urn = Urn(remote_path)
        if self.is_dir(urn.path()):
            raise OptionNotValid(name='remote_path', value=remote_path)

        if not self.check(urn.path()):
            raise RemoteResourceNotFound(urn.path())

        response = self.execute_request(action='download', path=urn.quote())
        buff.write(response.content)