Finalizes the download operation by doing various checks, such as format
    type, size check etc.
511613
def finalize_download(url, download_to_file, content_type, request):
    
    # If format is given, a format check is performed.
    if content_type and content_type not in request.headers['content-type']:
        msg = 'The downloaded file is not of the desired format'
        raise InvenioFileDownloadError(msg)

    # Save the downloaded file to desired or generated location.
    to_file = open(download_to_file, 'w')
    try:
        try:
            while True:
                block = request.read(CFG_FILEUTILS_BLOCK_SIZE)
                if not block:
                    break
                to_file.write(block)
        except Exception as e:
            msg = "Error when downloading %s into %s: %s" % \
                  (url, download_to_file, e)
            raise InvenioFileDownloadError(msg)
    finally:
        to_file.close()

    # Check Size
    filesize = os.path.getsize(download_to_file)
    if filesize == 0:
        raise InvenioFileDownloadError("%s seems to be empty" % (url,))

    # download successful, return the new path
    return download_to_file
647188
public void downloadAndExtract(DataSetType set) throws IOException {
        String localFilename = new File(remoteDataUrl(set)).getName();
        File tmpFile = new File(System.getProperty("java.io.tmpdir"), localFilename);
        File localCacheDir = getLocalCacheDir();

        // check empty cache
        if(localCacheDir.exists()) {
            File[] list = localCacheDir.listFiles();
            if(list == null || list.length == 0)
                localCacheDir.delete();
        }

        File localDestinationDir = new File(localCacheDir, dataSetName(set));
        if(!localDestinationDir.exists()) {
            localCacheDir.mkdirs();
            tmpFile.delete();
            log.info("Downloading dataset to " + tmpFile.getAbsolutePath());
            FileUtils.copyURLToFile(new URL(remoteDataUrl(set)), tmpFile);
        } else {
            //Directory exists and is non-empty - assume OK
            log.info("Using cached dataset at " + localCacheDir.getAbsolutePath());
            return;
        }

        if(expectedChecksum(set) != 0L) {
            log.info("Verifying download...");
            Checksum adler = new Adler32();
            FileUtils.checksum(tmpFile, adler);
            long localChecksum = adler.getValue();
            log.info("Checksum local is " + localChecksum + ", expecting "+expectedChecksum(set));

            if(expectedChecksum(set) != localChecksum) {
                log.error("Checksums do not match. Cleaning up files and failing...");
                tmpFile.delete();
                throw new IllegalStateException( "Dataset file failed checksum: " + tmpFile + " - expected checksum " + expectedChecksum(set)
                + " vs. actual checksum " + localChecksum + ". If this error persists, please open an issue at https://github.com/deeplearning4j/deeplearning4j.");
            }
        }

        try {
            ArchiveUtils.unzipFileTo(tmpFile.getAbsolutePath(), localCacheDir.getAbsolutePath());
        } catch (Throwable t){
            //Catch any errors during extraction, and delete the directory to avoid leaving the dir in an invalid state
            if(localCacheDir.exists())
                FileUtils.deleteDirectory(localCacheDir);
            throw t;
        }
    }
835119
public synchronized void finish() {
    if (!this.isInitialized()) {
      throw new IllegalStateException("Torrent not yet initialized!");
    }

    if (!this.isComplete()) {
      throw new IllegalStateException("Torrent download is not complete!");
    }

    eventDispatcher.multicaster().downloadComplete();
    setClientState(ClientState.SEEDING);
  }
279875
public void validInfoOnCompleted(@NonNull DownloadTask task, @NonNull DownloadStore store) {
        BreakpointInfo info = store.getAfterCompleted(task.getId());
        if (info == null) {
            info = new BreakpointInfo(task.getId(), task.getUrl(), task.getParentFile(),
                    task.getFilename());
            final long size;
            if (Util.isUriContentScheme(task.getUri())) {
                size = Util.getSizeFromContentUri(task.getUri());
            } else {
                final File file = task.getFile();
                if (file == null) {
                    size = 0;
                    Util.w(TAG, "file is not ready on valid info for task on complete state "
                            + task);
                } else {
                    size = file.length();
                }
            }
            info.addBlock(new BlockInfo(0, size, size));
        }
        DownloadTask.TaskHideWrapper.setBreakpointInfo(task, info);
    }
103881
def _finalize_azure_file(self, sd, metadata, digest):
        # type: (SyncCopy, blobxfer.models.synccopy.Descriptor, dict,
        #        str) -> None
        
        # set file properties if required
        if (blobxfer.util.is_not_empty(digest) or
                sd.dst_entity.cache_control is not None):
            blobxfer.operations.azure.file.set_file_properties(
                sd.dst_entity, digest)
            if blobxfer.util.is_not_empty(sd.dst_entity.replica_targets):
                for ase in sd.dst_entity.replica_targets:
                    blobxfer.operations.azure.file.set_file_properties(
                        ase, digest)
        # set file metadata if needed
        if blobxfer.util.is_not_empty(metadata):
            blobxfer.operations.azure.file.set_file_metadata(
                sd.dst_entity, metadata)
            if blobxfer.util.is_not_empty(sd.dst_entity.replica_targets):
                for ase in sd.dst_entity.replica_targets:
                    blobxfer.operations.azure.file.set_file_metadata(
                        ase, metadata)
1268620
def _verify_download_complete(self):
        
        if self.actual_bytes_read > self.bytes_to_read:
            raise TooLargeChunkDownloadError(self.actual_bytes_read, self.bytes_to_read, self.local_path)
        elif self.actual_bytes_read < self.bytes_to_read:
            raise PartialChunkDownloadError(self.actual_bytes_read, self.bytes_to_read, self.local_path)
796660
def _is_download(ending):
    """"""
    list = [
            'PDF',
            'DOC',
            'TXT',
            'PPT',
            'XLSX',
            'MP3',
            'SVG',
            '7Z',
            'HTML',
            'TEX',
            'MPP',
            'ODT',
            'RAR',
            'ZIP',
            'TAR',
            'EPUB',
           ]
    list_regex = [
                  'PDF'
                 ]
    if ending in list:
        return True
    for file_type in list_regex:
        if re.search(re.escape(file_type), ending):
            return True
    return False
1174309
protected function submitDownload()
    {
        if ($this->isPosted('install') && $this->validateDownload()) {
            $this->controlAccess('module_installer_download');
            $this->install_model->fromUrl($this->getSubmitted('sources'));
        }
    }
1264817
function downloadFiles(job, iFile, cb) {
   if (isUndefined(cb)) {
      cb = iFile;
      iFile = 0;
   }

   if (iFile === job.document.files.length) {
      cb();
      return;
   } 

   var file = job.document.files[iFile];
   var filePath = path.resolve(job.document.workingDirectory || '', file.fileName);

   if (isUndefined(file.fileSource) || isUndefined(file.fileSource.url)) {
      job.inProgress({ operation: job.operation, step: 'download error, rollback pending', fileName: file.fileName }, function(err) {
         showJobsError(err);
         cb(new Error('fileSource url missing'));
      });
      return;
   }

   job.inProgress({ step: 'downloading', fileName: file.fileName }, function(err) {
      showJobsError(err);
      downloadFile(file.fileSource.url, filePath, function(downloadError) {
         if (isUndefined(downloadError)) {
            validateChecksum(filePath, file.checksum, function(checksumError) {
               if (isUndefined(checksumError)) {
                  validateSignature(filePath, file.signature, function(signatureError) {
                     if (isUndefined(signatureError)) {
                        downloadFiles(job, iFile + 1, cb);
                     } else {
                        cb(signatureError);
                     }
                  });
               } else {
                  cb(checksumError);
               }
            });
         } else {
            cb(downloadError);
         }
      });
   });
}
183581
def _finalize_write(self):
        ''

        # Make sure we have written the header
        if not self._done_header:
            self._write_header()

        # Write the remaining decrypted part to disk
        block = self._crypto.encrypt(self._decrypted_buffer)
        self._decrypted = ''
        self._fp.write(block)
        self._checksumer.update(block)

        # Write the final checksum
        self._fp.write(self._checksumer.digest())
        self._valid = True
122835
def check_downloaded_files_sizes(self):
        
        for file_url in self.file_urls:
            local_path = file_url.get_local_path(self.dest_directory)
            self.check_file_size(file_url.size, local_path)
180298
void postFailure(final DownloadRequest request, final int statusCode, final String errMsg) {
    downloadPoster.execute(new Runnable() {
      @Override public void run() {
        request.downloadCallback().onFailure(request.downloadId(), statusCode, errMsg);
      }
    });
  }
1276388
def finalize(self):
        
        self._check_finalized()
        self._final = True

        for dest_w, values in self.dest_instrs_info.items():
            mux_vals = dict(zip(self.instructions, values))
            dest_w <<= sparse_mux(self.signal_wire, mux_vals)
1298101
def _download(self, resource):
    """"""
    if isinstance(resource, six.string_types):
      resource = resource_lib.Resource(url=resource)
    url = resource.url
    if url in self._sizes_checksums:
      expected_sha256 = self._sizes_checksums[url][1]
      download_path = self._get_final_dl_path(url, expected_sha256)
      if not self._force_download and resource.exists_locally(download_path):
        logging.info('URL %s already downloaded: reusing %s.',
                     url, download_path)
        self._recorded_sizes_checksums[url] = self._sizes_checksums[url]
        return promise.Promise.resolve(download_path)
    # There is a slight difference between downloader and extractor here:
    # the extractor manages its own temp directory, while the DownloadManager
    # manages the temp directory of downloader.
    download_dir_path = os.path.join(
        self._download_dir,
        '%s.tmp.%s' % (resource_lib.get_dl_dirname(url), uuid.uuid4().hex))
    tf.io.gfile.makedirs(download_dir_path)
    logging.info('Downloading %s into %s...', url, download_dir_path)

    def callback(val):
      checksum, dl_size = val
      return self._handle_download_result(
          resource, download_dir_path, checksum, dl_size)
    return self._downloader.download(url, download_dir_path).then(callback)
1120489
private void doRelease() {
        try {
            in.close();
        } catch (Exception ex) {
            if (log.isDebugEnabled())
                log.debug("FYI", ex);
        }
        if (in instanceof Releasable) {
            // This allows any underlying stream that has the close operation
            // disabled to be truly released
            Releasable r = (Releasable)in;
            r.release();
        }
        abortIfNeeded();
    }
449255
def finish():
    """"""
    out.warn("Interrupted!")
    for t in threads:
        t.stop()
    jobs.clear()
    out.warn("Waiting for download threads to finish.")
85312
void postProgress(final DownloadRequest request, final long bytesWritten, final long totalBytes) {
    downloadPoster.execute(new Runnable() {
      @Override public void run() {
        request.downloadCallback().onProgress(request.downloadId(), bytesWritten, totalBytes);
      }
    });
  }