Example #1
0
//! Added by Amir Krifa.
//! Used to load a Meta Info File, not in a network manner.
void MetainfoSocket::LoadMetaInfoFile(bool bSeeding,string MetaInfoFilePath)
{
	this->SetMetaInfoFileToLoad(MetaInfoFilePath);
	try
	{
		FILE *fil = fopen(m_filename.c_str(), "rb");
		m_fil=fil;
		if (fil)
		{
			PeerHandler& ref = static_cast<PeerHandler&>(Handler());
			BString meta;
			meta.read_file(fil,m_filename);
			fclose(fil);
			std::string info_hash = meta.GetHashAsString("info");
			// copy metainfo file
			std::string copy_to = ref.GetTorrentDirectory() + "\\" + info_hash + "\\.metainfo";
			
			ref.mkpath(copy_to);
			fil = fopen(m_filename.c_str(), "rb");
			if (fil)
			{
				FILE *fil2 = fopen(copy_to.c_str(), "wb");
				char buf[1000];
				size_t n = fread(buf, 1, 1000, fil);
				while (n > 0)
				{
					fwrite(buf, 1, n, fil2);
					n = fread(buf, 1, 1000, fil);
				}
				fclose(fil2);
				fclose(fil);
			}
			if (ref.SessionExists(info_hash))
			{
			}
			else
			{
				Session *sess = new Session(dynamic_cast<SocketHandler&>(Handler()), info_hash, bSeeding,string(ref.GetTorrentDirectory() + "\\" + info_hash + "\\" ));
				ref.RegSession( sess );
				sess -> SetHash(meta.GetHash("info"));
				
				BTString *p;
				if ((p = meta.GetString("announce")) != NULL)
				{
					sess -> SetAnnounce(p -> GetValue());
				}
				std::string name;
				if ((p = meta.GetString("info.name")) != NULL)
				{
					sess -> SetName(name = p -> GetValue());
				}
				BTInteger *piecelength = meta.GetInteger("info.piece length");
				if (piecelength)
				{
					sess -> SetPieceLength(piecelength -> GetVal());
				}
				BTInteger *length = meta.GetInteger("info.length");
				if (length)
				{
					sess -> AddFile(length -> GetVal());
				}
				else // info.files
				{
					BTObject *p = meta.GetBTObject("info.files");
					BTList *files = dynamic_cast<BTList *>(p);
					if (files)
					{
						btobject_v& ref = files -> GetList();
						for (btobject_v::iterator it = ref.begin(); it != ref.end(); it++)
						{
							BTDictionary *p = dynamic_cast<BTDictionary *>(*it);
							if (p)
							{
								BTInteger *length = dynamic_cast<BTInteger *>(p -> Find("length"));
								BTList *path = dynamic_cast<BTList *>(p -> Find("path"));
								if (path && length)
								{
									btobject_v& ref = path -> GetList();
									std::string pathname = name;
									for (btobject_v::iterator it = ref.begin(); it != ref.end(); it++)
									{
										BTString *p = dynamic_cast<BTString *>(*it);
										if (p)
											pathname += "\\" + p -> GetValue();
									}
									sess -> AddFile(pathname, length -> GetVal());
								}
								path=NULL;
								length=NULL;
							}
							p=NULL;
						}
					}
					files=NULL;
				}
				//! pieces checksum
				BTString *pieces = meta.GetString("info.pieces");
				if (pieces)
				{
					sess -> SetPieces( pieces -> GetValue() );
				}
				pieces =NULL;
				sess->SetFileToSeedPath(MetaInfoFilePath.substr(0,MetaInfoFilePath.find_last_of(".")));
				if(sess->isSeeding())
				{
					
					//! Copy the file to seed to the Torrents Directory
					string SourceFile(sess->GetFileToSeedPath());
					string DestFile(ref.GetTorrentDirectory() + "\\" + info_hash + "\\"+sess->GetName());
					if(CopyFile(ConvertLPCSTRToLPWSTR((char*)SourceFile.c_str()),ConvertLPCSTRToLPWSTR((char *)DestFile.c_str()),FALSE)==0)
					{
						
						//! Source File Not Found
						MessageBox( NULL,TEXT("Error encountred while trying to copy the file to seed."),
                            TEXT("Error while copying the source file."), MB_OK|MB_ICONSTOP );
               
					}
						
				}
				sess -> CreateFileManager();
				//! restore session
				sess -> Load();
				sess->Verify();
				sess=NULL;

			}
			
		}
	}
	catch (string)
	{
	}
	
}