Exemplo n.º 1
0
bool Downloader::downloadFile(NetFile *netFile)
{
    BYTE  *buffer = new BYTE[readBufferSize];
    DWORD bytesRead;
    File  file;

    updateFileName(netFile);
    updateStatus(msg("Connecting..."));
    setMarquee(true, false);

    try
    {
        netFile->open(internet);
    }
    catch(exception &e)
    {
        setMarquee(false, stopOnError ? (netFile->size == FILE_SIZE_UNKNOWN) : false);
        updateStatus(msg(e.what()));
        storeError(msg(e.what()));
        delete[] buffer;
        return false;
    }

    if(!netFile->handle)
    {
        setMarquee(false, stopOnError ? (netFile->size == FILE_SIZE_UNKNOWN) : false);
        updateStatus(msg("Cannot connect"));
        storeError();
        delete[] buffer;
        return false;
    }

    if(!file.open(netFile->name))
    {
        setMarquee(false, stopOnError ? (netFile->size == FILE_SIZE_UNKNOWN) : false);
        tstring errstr = msg("Cannot create file") + _T(" ") + netFile->name;
        updateStatus(errstr);
        storeError(errstr);
        delete[] buffer;
        return false;
    }

    Timer progressTimer(100);
    Timer speedTimer(1000);

    updateStatus(msg("Downloading..."));

    if(!(netFile->size == FILE_SIZE_UNKNOWN))
        setMarquee(false, false);

    processMessages();

    while(true)
    {
        if(downloadCancelled)
        {
            file.close();
            netFile->close();
            delete[] buffer;
            return true;
        }

        if(!netFile->read(buffer, readBufferSize, &bytesRead))
        {
            setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN);
            updateStatus(msg("Download failed"));
            storeError();
            file.close();
            netFile->close();
            delete[] buffer;
            return false;
        }

        if(bytesRead == 0)
            break;

        file.write(buffer, bytesRead);

        if(progressTimer.elapsed())
            updateProgress(netFile);

        if(speedTimer.elapsed())
            updateSpeed(netFile, &speedTimer);

        if(sizeTimeTimer.elapsed())
            updateSizeTime(netFile, &sizeTimeTimer);

        processMessages();
    }

    updateProgress(netFile);
    updateSpeed(netFile, &speedTimer);
    updateSizeTime(netFile, &sizeTimeTimer);
    updateStatus(msg("Download complete"));
    processMessages();

    file.close();
    netFile->close();
    netFile->downloaded = true;

    delete[] buffer;
    return true;
}
Exemplo n.º 2
0
bool Downloader::downloadFiles(bool useComponents)
{
    if(ownMsgLoop)
        downloadCancelled = false;

    if(files.empty() && ftpDirs.empty())
        return true;

    setMarquee(true);

    processFtpDirs();

    if(getFileSizes() == OPERATION_STOPPED)
    {
        TRACE(_T("OPERATION_STOPPED"));
        setMarquee(false);
        return false;
    }

    TRACE(_T("filesSize: %d"), (DWORD)filesSize);

    if(!openInternet())
    {
        storeError();
        setMarquee(false);
        return false;
    }

    sizeTimeTimer.start(500);
    updateStatus(msg("Starting download..."));
    TRACE(_T("Starting file download cycle..."));

    if(!(filesSize == FILE_SIZE_UNKNOWN))
        setMarquee(false);

    processMessages();

    for(map<tstring, NetFile *>::iterator i = files.begin(); i != files.end(); i++)
    {
        NetFile *file = i->second;

        if(downloadCancelled)
            break;

        if(useComponents)
            if(!file->selected(components))
                continue;

        if(!file->downloaded)
        {
            // If mirror was used in getFileSizes() function, check mirror first:
            if(file->mirrorUsed.length())
            {
                NetFile newFile(file->mirrorUsed, file->name, file->size);

                if(downloadFile(&newFile))
                {
                    file->downloaded = newFile.downloaded;
                    file->bytesDownloaded = newFile.bytesDownloaded;
                    downloadedFilesSize += file->bytesDownloaded;
                    continue;
                }
            }

            if(!downloadFile(file))
            {
                TRACE(_T("File was not downloaded."));

                if(checkMirrors(i->first, true))
                    downloadedFilesSize += file->bytesDownloaded;
                else
                {
                    if(stopOnError)
                    {
                        closeInternet();
                        return false;
                    }
                    else
                    {
                        TRACE(_T("Ignoring file %s"), file->name.c_str());
                    }
                }
            }
            else
                downloadedFilesSize += file->bytesDownloaded;
        }

        processMessages();
    }

    closeInternet();
    return filesDownloaded();
}
Exemplo n.º 3
0
void ReadableJSStream::storeException(JSC::ExecState& state)
{
    JSValue exception = state.exception()->value();
    state.clearException();
    storeError(state, exception);
}
Exemplo n.º 4
0
DWORDLONG Downloader::getFileSizes(bool useComponents)
{
    if(ownMsgLoop)
        downloadCancelled = false;

    if(files.empty())
        return 0;

    updateStatus(msg("Initializing..."));
    processMessages();

    if(!openInternet())
    {
        storeError();
        return FILE_SIZE_UNKNOWN;
    }

    filesSize = 0;
    bool sizeUnknown = false;

    for(map<tstring, NetFile *>::iterator i = files.begin(); i != files.end(); i++)
    {
        updateStatus(msg("Getting file information..."));
        processMessages();

        NetFile *file = i->second;

        if(downloadCancelled)
            break;

        if(useComponents)
            if(!file->selected(components))
                continue;

        if(file->size == FILE_SIZE_UNKNOWN)
        {
            try
            {
                try
                {
                    updateFileName(file);
                    processMessages();
                    file->size = file->url.getSize(internet);
                }
                catch(HTTPError &e)
                {
                    updateStatus(msg(e.what()));
                    //TODO: if allowContinue==0 & error code == file not found - stop.
                }
                
                if(file->size == FILE_SIZE_UNKNOWN)
                    checkMirrors(i->first, false);
            }
            catch(FatalNetworkError &e)
            {
                updateStatus(msg(e.what()));
                storeError(msg(e.what()));
                closeInternet();
                return OPERATION_STOPPED;
            }
        }

        if(!(file->size == FILE_SIZE_UNKNOWN))
            filesSize += file->size;
        else
            sizeUnknown = true;
    }

    closeInternet();

    if(sizeUnknown && !filesSize)
        filesSize = FILE_SIZE_UNKNOWN; //TODO: if only part of files has unknown size - ???

#ifdef _DEBUG
    TRACE(_T("getFileSizes result:"));

    for(map<tstring, NetFile *>::iterator i = files.begin(); i != files.end(); i++)
    {
        NetFile *file = i->second;
        TRACE(_T("    %s: %s"), file->getShortName().c_str(), (file->size == FILE_SIZE_UNKNOWN) ? _T("Unknown") : itotstr((DWORD)file->size).c_str()); 
    }
#endif

    return filesSize;
}
Exemplo n.º 5
0
bool Downloader::downloadFile(NetFile *netFile)
{
	BYTE  buffer[READ_BUFFER_SIZE];
	DWORD bytesRead;
	File  file;

	updateFileName(netFile);
	updateStatus(msg("Connecting..."));
	setMarquee(true, false);

	try
	{
		netFile->open(internet);
	}
	catch(exception &e)
	{
		setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN);
		updateStatus(msg(e.what()));
		storeError(msg(e.what()));
		return false;
	}

	if(!netFile->handle)
	{
		setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN);
		updateStatus(msg("Cannot connect"));
		storeError();
		return false;
	}

	file.open(netFile->name);

	Timer progressTimer(100);
	Timer speedTimer(1000);

	updateStatus(msg("Downloading..."));

	if(!(netFile->size == FILE_SIZE_UNKNOWN))
		setMarquee(false, false);

	while(true)
	{
		if(downloadCancelled)
		{
			file.close();
			netFile->close();
			return true;
		}

		if(!netFile->read(buffer, READ_BUFFER_SIZE, &bytesRead))
		{
			setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN);
			updateStatus(msg("Download failed"));
			storeError();
			file.close();
			netFile->close();
			return false;
		}

		if(bytesRead == 0)
			break;

		file.write(buffer, bytesRead);

		if(progressTimer.elapsed())
			updateProgress(netFile);

		if(speedTimer.elapsed())
			updateSpeed(netFile, &speedTimer);

		if(sizeTimeTimer.elapsed())
			updateSizeTime(netFile, &sizeTimeTimer);
	}

	updateProgress(netFile);
	updateSpeed(netFile, &speedTimer);
	updateSizeTime(netFile, &sizeTimeTimer);
	updateStatus(msg("Download complete"));

	file.close();
	netFile->close();
	netFile->downloaded = true;

	return true;
}