当前位置: 首页>>代码示例>>C#>>正文


C# DB.Upload方法代码示例

本文整理汇总了C#中DB.Upload方法的典型用法代码示例。如果您正苦于以下问题:C# DB.Upload方法的具体用法?C# DB.Upload怎么用?C# DB.Upload使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DB的用法示例。


在下文中一共展示了DB.Upload方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddFile

		public static DBFile AddFile (this DBWork me, DB db, string path, string filename, bool hidden, string compressed_mime)
		{
			DBFile result = db.Upload (path, Path.GetExtension (filename), hidden, compressed_mime);
			me.AddFile (db, result, filename, hidden);
			return result;
		}
开发者ID:DavidS,项目名称:monkeywrench,代码行数:6,代码来源:DBWork_Extensions.cs

示例2: MoveFilesToFileSystem

		public static int MoveFilesToFileSystem ()
		{
			long moved_bytes = 0;

			Manager.log.Info ("MoveFilesToFileSystem: [START]");

			using (DB db = new DB ()) {
				using (DB download_db = new DB ()) {
					while (true) {
						using (IDbCommand cmd = db.CreateCommand ()) {
							// execute this in chunks to avoid huge data transfers and slowdowns.
							cmd.CommandText = "SELECT * FROM File WHERE NOT file_id IS NULL LIMIT 100";
							using (IDataReader reader = cmd.ExecuteReader ()) {
								if (!reader.Read ())
									break;

								do {
									DBFile file = new DBFile (reader);
									byte [] buffer = new byte [1024];
									int oid = file.file_id.Value;
									int read;
									string fn = FileUtilities.CreateFilename (file.md5, file.compressed_mime == MimeTypes.GZ, true);
									using (FileStream writer = new FileStream (fn, FileMode.Create, FileAccess.Write, FileShare.Read)) {
										using (Stream str = download_db.Download (file)) {
											while ((read = str.Read (buffer, 0, buffer.Length)) != 0)
												writer.Write (buffer, 0, read);
										}
									}

									IDbTransaction transaction = download_db.BeginTransaction ();
									download_db.Manager.Delete (oid);
									file.file_id = null;
									file.Save (download_db);
									transaction.Commit ();

									moved_bytes += file.size;
									log.InfoFormat ("MoveFilesToFileSystem: Moved oid {0} to {1} ({2} bytes, {3} total bytes moved)", oid, fn, file.size, moved_bytes);
								} while (reader.Read ());
							}
						}
					}

					while (true) {
						using (IDbCommand cmd = db.CreateCommand ()) {
							// execute this in chunks to avoid huge data transfers and slowdowns.
							cmd.CommandText = "SELECT * FROM Revision WHERE (diff_file_id IS NULL AND NOT diff = '') OR (log_file_id IS NULL AND NOT log = '') LIMIT 100";
							using (IDataReader reader = cmd.ExecuteReader ()) {
								if (!reader.Read ())
									break;

								do {
									DBRevision revision = new DBRevision (reader);
									string tmpfile = null;

									if (!string.IsNullOrEmpty (revision.diff)) {
										int length = 0;
										if (revision.diff_file_id == null) {
											try {
												length = revision.diff.Length;
												tmpfile = Path.GetTempFileName ();
												File.WriteAllText (tmpfile, revision.diff);
												DBFile diff = download_db.Upload (tmpfile, ".log", false, null);
												revision.diff_file_id = diff.id;
												revision.diff = null;
											} finally {
												try {
													if (File.Exists (tmpfile))
														File.Delete (tmpfile);
												} catch (Exception ex) {
													log.ErrorFormat ("error deleting temp file: {0}", ex);
												}
											}
											moved_bytes += length;
											log.InfoFormat ("MoveFilesToFileSystem: Moved revision {0}'s diff to db/filesystem ({1} bytes, {2} total bytes moved)", revision.id, length, moved_bytes);
										}
									}

									if (!string.IsNullOrEmpty (revision.log)) {
										int length = 0;
										if (revision.log_file_id == null) {
											try {
												length = revision.log.Length;
												tmpfile = Path.GetTempFileName ();
												File.WriteAllText (tmpfile, revision.log);
												DBFile log = download_db.Upload (tmpfile, ".log", false, null);
												revision.log_file_id = log.id;
												revision.log = null;
											} finally {
												try {
													if (File.Exists (tmpfile))
														File.Delete (tmpfile);
												} catch (Exception ex) {
													log.ErrorFormat ("error deleting temp file: {0}", ex);
												}
											}
											moved_bytes += length;
											Manager.log.InfoFormat ("MoveFilesToFileSystem: Moved revision {0}'s log to db/filesystem ({1} bytes, {2} total bytes moved)", revision.id, length, moved_bytes);
										}
										revision.log = null;
									}
//.........这里部分代码省略.........
开发者ID:joewstroman,项目名称:monkeywrench,代码行数:101,代码来源:Manager.cs

示例3: ExecuteRequest

		private static void ExecuteRequest (object state)
		{
			NetworkStream stream = null;
			byte [] buffer;
			int id = counter++;
			string tmpfile = null;
			TcpClient client = null;
			BinaryReader reader = null;
			BinaryWriter writer = null;
			string remote_ip;
			WebServiceLogin login = new WebServiceLogin ();

			try {
				buffer = new byte [1024];

				client = (TcpClient) state;
				stream = client.GetStream ();
				stream.ReadTimeout = (int) TimeSpan.FromMinutes (5).TotalMilliseconds;
				stream.WriteTimeout = stream.ReadTimeout;
				reader = new BinaryReader (stream);
				writer = new BinaryWriter (stream);
				remote_ip = ((IPEndPoint) client.Client.RemoteEndPoint).Address.ToString ();

				/* Format:
				 * Field                   Length in bytes    Description
				 * version                       1                  1
				 * name_length                   1
				 * name                       <name_length>
				 * password_length               1
				 * password                 <password_length>
				 * work_id                       4                 The Work.id field in the database
				 * file_count                    2
				 * reserved                      8
				 *  
				 * < file #1>
				 * marker                       12               'MonkeyWrench'
				 * md5                          16
				 * flags                         1                  1 = compressed, 2 = hidden  
				 * filename_length               1
				 * filename               <filename_length> 
				 * <client waits for answer, either type 2 or 4, type 2: server already has data, type 4: server does not have data>        
				 * [type: 4] compressed_mime_length        1	
				 * [type: 4] compressed_mime   <compressed_mime_length>
				 * [type: 4] content_length      4
				 * [type: 4] content       <content_length>
				 * [type: 4] <client waits for response, type 2>
				 * 
				 * < file #2 >
				 * ...
				 * 
				 * The response data has this format:
				 * version                        1
				 * type                           1                  1 = everything OK, 2 = file received OK, 3 = error, 4 = send file
				 *  depending on type, here are the subsequent fields:
				 * [type: 3] message_length       2
				 * [type: 3] message          <message length>
				 * 
				 */
				byte version = reader.ReadByte ();
				byte name_length = reader.ReadByte ();
				login.User = ReadString (reader, buffer, name_length);
				byte password_length = reader.ReadByte ();
				login.Password = ReadString (reader, buffer, password_length);
				login.Ip4 = remote_ip;
				int work_id = reader.ReadInt32 ();
				ushort file_count = reader.ReadUInt16 ();
				reader.ReadInt64 ();

				Logger.Log (2, "Upload.ExecuteRequest (): {0} version: {1} work_id: {2} file count: {3} remote ip: {4}", id, version, work_id, file_count, client.Client.RemoteEndPoint.ToString ());

				using (DB db = new DB ()) {
					Authentication.VerifyUserInRole (remote_ip, db, login, Roles.BuildBot, true);
					for (ushort i = 0; i < file_count; i++) {
						byte [] md5 = new byte [16];
						byte flags;
						bool hidden;
						byte filename_length;
						byte compressed_mime_length;
						int content_length;
						string filename;
						string compressed_mime;
						string marker;
						
						marker = ReadString (reader, buffer, 12);
						if (marker != "MonkeyWrench")
							throw new Exception (string.Format ("Didn't get marker 'MonkeyWrench' at start of file, got '{0}'", marker));

						reader.Read (md5, 0, 16);

						flags = reader.ReadByte ();
						filename_length = reader.ReadByte ();

						filename = ReadString (reader, buffer, filename_length);

						hidden = (flags & 0x2) == 0x2;
						// compressed = (flags & 0x1) == 0x1;

						Logger.Log (2, "Upload.ExecuteRequest (): {0} file #{1}: filename: '{2}' ", id, i + 1, filename);

						DBFile file = DBFile_Extensions.Find (db, FileUtilities.MD5BytesToString (md5));
//.........这里部分代码省略.........
开发者ID:vargaz,项目名称:monkeywrench,代码行数:101,代码来源:Upload.cs


注:本文中的DB.Upload方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。