Exemple #1
0
void CzHttp::SetFtpDir( char *name )
{
    // FTPカレントディレクトリを変更
    //
    if ( mode != CZHTTP_MODE_FTPREADY ) {
        return;
    }
    FtpSetCurrentDirectory( hService, name );
    GetFtpResponse();
}
bool FTP_Download( FTP_Details ftp, char* remotename, char* localname, char* remote_dir )
{
	std::string str, dir;
	char message[256];
	
	HINTERNET hOpen = InternetOpen( "FTP Download", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if( !hOpen )
	{
		MessageBox( GetActiveWindow(), "File could not be found, The application will now exit", "Missing File Error", MB_OK | MB_ICONHAND );
		return false;
	}

    HINTERNET hConnect = InternetConnect( hOpen, ftp.servername, ftp.port,
                                ftp.username, ftp.password, INTERNET_SERVICE_FTP,
                                INTERNET_FLAG_PASSIVE, 0 );
    if( !hConnect )
	{
		MessageBox( GetActiveWindow(), "File could not be found, The application will now exit", "Missing File Error", MB_OK | MB_ICONHAND );
		return false;
	}

	char remdir[255];
	strcpy_s( remdir, ftp.directory );
	strcat_s( remdir, remote_dir );
	FtpSetCurrentDirectory( hConnect, remdir );

	str = localname;
	if( str.find( "/" ) ) dir = str.substr( 0, str.find( "/" ) );
	if( !FileExists( (char*)dir.c_str() ) ) CreateDirectory( (char*)dir.c_str(), NULL );

	sprintf_s( message, "Could not find file '%s'\nNow attempting to download the file.", remotename );
	MessageBox( GetActiveWindow(), message, "Missing File Error", MB_OK | MB_ICONEXCLAMATION );

	BOOL lRes = FtpGetFile( hConnect, remotename, localname, false,
                            FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0 );
    if( !lRes )
	{
		MessageBox( GetActiveWindow(), "File could not be downloaded, The application will now exit", "Missing File Error", MB_OK | MB_ICONHAND );
		return false;
	}

	InternetCloseHandle( hConnect );
	InternetCloseHandle( hOpen );

	MessageBox( GetActiveWindow(), "File sucessfully downloaded", "Missing File Error", MB_OK | MB_ICONASTERISK );

	return true;
}
Exemple #3
0
/*
  Name: fdel()

  Description: deletes file from ftp.

  Example: fdel("ftp://*****:*****@ftp.server.org/dir/file.ext");

  Parameters:
         [in] void *param - see example.

  Return value:
          ON SUCCESS - '1'.
          ON   ERROR - '0'.

  Revision: 16.01.2007
*/
unsigned long __stdcall net::fdel(void *param)
{
  char *par = (char *)param;

  InterlockedExchange((long *)&net::last_err, (long)net::WAIT);

  if(par == NULL)
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    return 0;
  }

  if(*(char *)par == 0)
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    return 0;
  }
  
  char root_dir[] = "/";

  // user
  char *user = strstr(par, "ftp://");
  if(NULL == user)
  {
    user = par;
  }
  else
  {
    user = par + lstrlen("ftp://");
  }

  // pass
  char *pass = user;
  while((*pass != 0x00) && (*pass != ':')) pass++;
  if(*pass == ':') // delimiter ':' found
  {
    *pass = 0x00;
    pass++;
  }
  else
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  // host
  char *host = pass;
  while((*host != 0x00) && (*host != '@')) host++;
  if(*host == '@') // delimiter ':' found
  {
    *host = 0x00;
    host++;
  }
  else
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  // dir
  char *dir = host;
  while((*dir != 0x00) && (*dir != '/')) dir++;
  if(*dir == '/')
  {
    *dir = 0x00;
    dir++;
  }
  else
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  // file
  char *remote_file = dir;
  // forward to end
  while(*remote_file != 0x00) remote_file++;
  // back to last '/'
  while((remote_file != dir) && (*remote_file != '/')) remote_file--;
  if(*remote_file == '/')
  {
    *remote_file = 0x00;
    remote_file++;
  }
  else // remote file in root dir
  {
    dir = root_dir;
  }
  
  HINTERNET hInternet = ::InternetOpen(HTTP_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  if(hInternet == NULL)
  {
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

    HINTERNET hConnect = ::InternetConnect(hInternet, host, INTERNET_INVALID_PORT_NUMBER, user, pass, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
  if(hConnect == NULL)
  {
    ::InternetCloseHandle(hConnect);
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  if(!FtpSetCurrentDirectory(hConnect, dir))
  {
    ::InternetCloseHandle(hConnect);
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  if(!FtpDeleteFile(hConnect, remote_file))
  {
    ::InternetCloseHandle(hConnect);
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  ::InternetCloseHandle(hConnect);
  ::InternetCloseHandle(hInternet);
  InterlockedExchange((long *)&net::last_err, (long)net::OK);
  if(par != NULL) delete[]par;

  return 1;
}
Exemple #4
0
/*
  Name: fput()

  Description: puts local file to ftp.

  Example: fput("ftp://*****:*****@ftp.server.org/dir/file.ext c:\file.ext");

  Parameters:
         [in] void *param - see example.

  Return value:
          ON SUCCESS - '1'.
          ON   ERROR - '0'.

  Revision: 16.01.2007
*/
unsigned long __stdcall net::fput(void *param)
{
  char *par = (char *)param;

  InterlockedExchange((long *)&net::last_err, (long)net::WAIT);

  if(param == NULL)
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    return 0;
  }

  if(*(char *)param == 0)
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    return 0;
  }

  char root_dir[] = "/";

  // user
  char *user = strstr(par, "ftp://");
  if(NULL == user)
  {
    user = par;
  }
  else
  {
    user = par + lstrlen("ftp://");
  }

  // pass
  char *pass = user;
  while((*pass != 0x00) && (*pass != ':')) pass++;
  if(*pass == ':') // delimiter ':' found
  {
    *pass = 0x00;
    pass++;
  }
  else
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  // host
  char *host = pass;
  while((*host != 0x00) && (*host != '@')) host++;
  if(*host == '@') // delimiter ':' found
  {
    *host = 0x00;
    host++;
  }
  else
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  // dir
  char *dir = host;
  while((*dir != 0x00) && (*dir != '/')) dir++;
  if(*dir == '/')
  {
    *dir = 0x00;
    dir++;
  }
  else
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  // files
  char *remote_file = dir;
  char *local_file  = dir;
  // find 1st space/tab
  while((*remote_file != 0x00) && (*remote_file != 0x20)) remote_file++;
  if(*remote_file == 0x20)
  {
    local_file = remote_file;
    // skip the space
    *local_file = 0x00;
    local_file++;
    // go backward to find 1st '/' from the end
    while((remote_file != dir) && (*remote_file != '/')) remote_file--;
    if(*remote_file == '/')
    {
      *remote_file = 0x00;
      remote_file++;
    }
    else
    {
      dir = root_dir;
    }
  }
  else
  {
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  HINTERNET hInternet = ::InternetOpen(HTTP_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  if(hInternet == NULL)
  {
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

    HINTERNET hConnect = ::InternetConnect(hInternet, host, INTERNET_INVALID_PORT_NUMBER, user, pass, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
  if(hConnect == NULL)
  {
    ::InternetCloseHandle(hConnect);
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  if(!FtpSetCurrentDirectory(hConnect, dir))
  {
    ::InternetCloseHandle(hConnect);
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }
  
  // NOTE: existing file with the same name on FTP server will be rewritten!
  if(!FtpPutFile(hConnect, local_file, remote_file, (INTERNET_FLAG_RELOAD | FTP_TRANSFER_TYPE_BINARY), 0))
  {
    ::InternetCloseHandle(hConnect);
    ::InternetCloseHandle(hInternet);
    InterlockedExchange((long *)&net::last_err, (long)net::ERR);
    if(par != NULL) delete[]par;
    return 0;
  }

  ::InternetCloseHandle(hConnect);
  ::InternetCloseHandle(hInternet);
  InterlockedExchange((long *)&net::last_err, (long)net::OK);
  if(par != NULL) delete[]par;
  return 1;
}
Exemple #5
0
bool Downloader::scanFtpDir(FtpDir *ftpDir, tstring destsubdir)
{
    Url url(ftpDir->url);
    url.internetOptions = internetOptions;
    
    updateFileName(url.components.lpszUrlPath);
    
    if(!url.connect(internet))
    {
        storeError();
        return false;
    }
    
    if(!FtpSetCurrentDirectory(url.connection, url.components.lpszUrlPath))
    {
        storeError();
        return false;
    }
    
    list<tstring> dirs;
    WIN32_FIND_DATA fd;

    TRACE(_T("Scanning FTP dir %s:"), ftpDir->url.c_str());
    HINTERNET handle = FtpFindFirstFile(url.connection, ftpDir->mask.c_str(), &fd, NULL, NULL);

    if(handle)
    {
        TRACE(_T("    (%s) %s"), (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _T("D") : _T("F"), fd.cFileName);
        updateFileName(tstring(fd.cFileName));

        if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            tstring dirname(fd.cFileName);

            if(!(dirname.compare(_T(".")) == 0) && !(dirname.compare(_T("..")) == 0))
                dirs.push_back(dirname);
        }
        else
        {
            tstring fileUrl  = addslash(ftpDir->url);
            tstring fileName = addbackslash(ftpDir->destdir);
            fileUrl  += tstring(fd.cFileName);
            fileName += addbackslash(destsubdir);
            fileName += tstring(fd.cFileName);
            
            addFile(fileUrl, fileName, ((DWORDLONG)fd.nFileSizeHigh << 32) | fd.nFileSizeLow, ftpDir->compstr);
        }

        while(InternetFindNextFile(handle, &fd))
        {
            TRACE(_T("    (%s) %s"), (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _T("D") : _T("F"), fd.cFileName);
            updateFileName(tstring(fd.cFileName));

            if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                tstring dirname(fd.cFileName);

                if(!(dirname.compare(_T(".")) == 0) && !(dirname.compare(_T("..")) == 0))
                    dirs.push_back(dirname);
            }
            else
            {
                tstring fileUrl  = addslash(ftpDir->url);
                tstring fileName = addbackslash(ftpDir->destdir);
                fileUrl  += tstring(fd.cFileName);
                fileName += addbackslash(destsubdir);
                fileName += tstring(fd.cFileName);
                
                addFile(fileUrl, fileName, ((DWORDLONG)fd.nFileSizeHigh << 32) | fd.nFileSizeLow, ftpDir->compstr);
            }
        }
    }

    url.disconnect();

    if(ftpDir->recursive && !dirs.empty())
    {
        for(list<tstring>::iterator i = dirs.begin(); i != dirs.end(); i++)
        {
            tstring dir = *i;

            tstring urlstr = addslash(ftpDir->url);
            urlstr += dir;
            FtpDir fdir(urlstr, ftpDir->mask, ftpDir->destdir, ftpDir->recursive, ftpDir->compstr);
            
            if(preserveFtpDirs)
            {
                tstring destdir(addbackslash(ftpDir->destdir));
                destdir += addbackslash(destsubdir);
                destdir += dir;
                TRACE(_T("Creating directory %s"), destdir.c_str());
                _tmkdir(destdir.c_str());

                tstring subdir = addbackslash(destsubdir);
                subdir += dir;
                scanFtpDir(&fdir, subdir);
            }
            else
                scanFtpDir(&fdir);
        }
    }

    return true;
}
Exemple #6
0
int main(int argc, char **argv)
{
	char * ftphost  = 0;
	char * ftpuser  = 0;
	char * ftppass  = 0;
	char * ftpdir   = "cam";
	char * pic      = "snap.png";
	char * vidsrc   = "camera"; // "camera_opt", if you want to control video props
	char * viddst   = "Video Renderer"; // "NullRenderer", if you don't want the window
	int  seconds    = 10;
	bool externalFTP = false;


	atexit(myexit);

	for ( int i=0; i<argc; i++ )
	{
		if ( ! strcmp(argv[i], "-host") )	ftphost = argv[++i];	
		if ( ! strcmp(argv[i], "-user") )	ftpuser = argv[++i];
		if ( ! strcmp(argv[i], "-pass") )	ftppass = argv[++i];
		if ( ! strcmp(argv[i], "-dir" ) )	ftpdir  = argv[++i];
		if ( ! strcmp(argv[i], "-pic" ) )	pic     = argv[++i];
		if ( ! strcmp(argv[i], "-vsrc" ) )	vidsrc	= argv[++i];
		if ( ! strcmp(argv[i], "-vdst" ) )	viddst  = argv[++i];		
		if ( ! strcmp(argv[i], "-time") )	seconds = atoi(argv[++i]);		
		if ( ! strcmp(argv[i], "-ext") )	externalFTP = true;;		
	}
	if ( !ftphost || !ftpuser || !ftppass )
	{
		printf( help() );
		return 1;
	}

	if ( externalFTP )
	{
		//
		// being lazy, i just remote-control ftp.exe...
		//
		// set up ftp - commands
		FILE * ft = fopen( "ftp.txt", "wb" );
		fprintf( ft, "%s\n%s\n", ftpuser, ftppass );
		if ( ftpdir )
		{
			fprintf( ft, "cd %s\n", ftpdir );
		}
		fprintf( ft, "binary\n" );
		fprintf( ft, "put %s\n", pic );
		fprintf( ft, "quit\n" );
		fclose( ft );
	}
	else
	{
		net = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT,"","",0);
		if ( ! net )
		{
			return 2;
		}

		con = InternetConnect( net, ftphost,21,ftpuser,ftppass,INTERNET_SERVICE_FTP,0,0);
		if ( ! con )
		{
			return 3;
		}

		if ( ftpdir )
		{
			FtpSetCurrentDirectory( con, ftpdir );
		}
	}
	// create & start filtergraph:
	grabber = Video::createGrabber(pic);
	if ( ! grabber )
	{
		return 4;
	}
	graph = Video::createFilterGraph();
	if ( ! graph )
	{
		return 5;
	}

	bool ok = graph->build( vidsrc, grabber, viddst );
	if( ok )
	{
		ok = graph->start();
		while( ok )
		{
			grabber->snap();
			Sleep(seconds * 1000); 
			if ( externalFTP )
			{
				char cmdline[300];
				sprintf( cmdline, "ftp -s:ftp.txt %s", ftphost );
				system( cmdline );
			}
			else
			{
				FtpPutFile( con, pic, pic, FTP_TRANSFER_TYPE_BINARY, 0 );
			}
		}
	}
	return 0;
}