public async Task SendFile(FileInfo fileInfo) { //创建一个文件对象 if (fileInfo.Name.Length > 50) { throw new Exception("文件名不能超过50."); } await Task.Run(() => { using (FileStream fStream = fileInfo.OpenRead()) { int PacketSize = 20480 - 200; //包的大小 string Id = Guid.NewGuid().ToString(); int PackageCount = (int)(fStream.Length / ((long)PacketSize)); //包的数量 int LastDataPacket = (int)(fStream.Length - ((long)(PacketSize * PackageCount))); //最后一个包的大小 FileRequestInfo fi = new FileRequestInfo() { FileName = fileInfo.Name, FileSize = fileInfo.Length, Id = Id, PackageCount = LastDataPacket == 0 ? PackageCount : PackageCount + 1 }; //数据包 //开始循环发送数据包 byte[] data = new byte[PacketSize]; for (int i = 0; i < PackageCount; i++) { //从文件流读取数据并填充数据包 fStream.Read(data, 0, data.Length); fi.PackData = data; fi.Seq = i; this.Send(fi.ToSendData(CommonCommands.IMFile, i)); } //如果还有多余的数据包,则应该发送完毕! if (LastDataPacket != 0) { data = new byte[LastDataPacket]; fStream.Read(data, 0, data.Length); fi.PackData = data; fi.Seq = fi.PackageCount == 1 ? 0 : fi.PackageCount - 1; this.Send(fi.ToSendData(CommonCommands.IMFile, PackageCount)); } //关闭套接字 //client.Close(); //关闭文件流 fStream.Close(); } }); //打开文件流 }
static void HandleDownload(NetworkStream stream, FileRequestInfo requestInfo) { var context = new SmartShareContext(); var file = context.UploadedFiles.First(x => x.Name == requestInfo.FileName); if (file == null) { stream.Write(new byte[] { FAIL_BYTE }); return; } if (!(file.Password == requestInfo.Password)) { stream.Write(new byte[] { FAIL_BYTE }); return; } if (file.DownloadsExceeded() || file.IsExpired()) { stream.Write(new byte[] { FAIL_BYTE }); stream.Close(); file.Delete(context); return; } stream.Write(new byte[] { ACK_BYTE }); file.DownloadCount++; context.UploadedFiles.Update(file); context.SaveChanges(); var connection = context.Database.GetDbConnection(); if (connection is NpgsqlConnection) { var conn = (NpgsqlConnection)connection; conn.Open(); var blobManager = new NpgsqlLargeObjectManager(conn); using (var transaction = connection.BeginTransaction()) { var blobStream = blobManager.OpenRead(file.BlobOid); blobStream.CopyTo(stream); transaction.Commit(); } conn.Close(); Console.WriteLine("Download successful."); } else { throw new NotSupportedException("Unsupported database adapter."); } }
public static bool Upload(FileInfo fileInfo, int?maxDownloads, int duration, string password) { try { var client = new TcpClient(HOST, PORT); using (var stream = client.GetStream()) { var requestSerializer = new XmlSerializer(typeof(FileRequestInfo)); var fileUploadInfo = new FileRequestInfo() { RequestType = RequestType.Upload, FileName = fileInfo.Name, DurationMinutes = duration, MaxDownloadCount = maxDownloads, Password = password, }; requestSerializer.Serialize(stream, fileUploadInfo); // Wait for server to acknowledge with a byte, then start streaming file. var response = new byte[1]; stream.Read(response); if (response[0] != ACK_BYTE) { return(false); } try { using (var fileStream = File.Open(fileInfo.FullName, FileMode.Open)) { using (GZipStream compressionStream = new GZipStream(stream, CompressionMode.Compress)) { fileStream.CopyTo(compressionStream); } } } catch (System.IO.FileNotFoundException e) { Console.WriteLine(e.Message); return(false); } } } catch { return(false); } return(true); }
public UploadedFile(FileRequestInfo requestInfo, uint oid) { BlobOid = oid; Name = requestInfo.FileName; CreatedTime = DateTime.Now; Password = requestInfo.Password; MaxDownloads = requestInfo.MaxDownloadCount; if (requestInfo.DurationMinutes < DURATION_MINIMUM || requestInfo.DurationMinutes > DURATION_MAXIMUM) { ExpireTime = DateTime.Now.AddMinutes(DURATION_DEFAULT); } else { ExpireTime = DateTime.Now.AddMinutes(requestInfo.DurationMinutes); } }
public static FileView View(string fileName, string password) { var client = new TcpClient(HOST, PORT); using (var stream = client.GetStream()) { var requestSerializer = new XmlSerializer(typeof(FileRequestInfo)); var fileViewInfo = new FileRequestInfo() { RequestType = RequestType.View, FileName = fileName, Password = password, }; requestSerializer.Serialize(stream, fileViewInfo); var responseSerializer = new XmlSerializer(typeof(FileView)); return((FileView)responseSerializer.Deserialize(stream)); } }
public static bool Download(string fileName, string password) { try { var client = new TcpClient(HOST, PORT); using (var stream = client.GetStream()) { var requestSerializer = new XmlSerializer(typeof(FileRequestInfo)); var fileUploadInfo = new FileRequestInfo() { RequestType = RequestType.Download, FileName = fileName, Password = password, }; // Serialize request to server requestSerializer.Serialize(stream, fileUploadInfo); var responseByte = new byte[1]; stream.Read(responseByte); if (responseByte[0] == FAIL_BYTE) { return(false); } using (FileStream decompressedFileStream = File.Create(fileName)) { using (GZipStream decompressionStream = new GZipStream(stream, CompressionMode.Decompress)) { decompressionStream.CopyTo(decompressedFileStream); } } } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
private void runArcgisRequest(FileRequestInfo tempFileInfo) { string agUrl = textBox1.Text; //for some reason the data is stored as cdata, so clean the payload & parse xml string cleanXmlStr = Regex.Replace(tempFileInfo.cdataPayload, @"\t|\n|\r", ""); XElement extentXml = XElement.Parse(cleanXmlStr); ExtentInfo cacheReqsExt = (from item in extentXml.DescendantsAndSelf() select new ExtentInfo { minLat = (string)item.Element("minLat"), maxLat = (string)item.Element("maxLat"), minLon = (string)item.Element("minLon"), maxLon = (string)item.Element("maxLon") }).First(); string agsPayload = agUrl + "/export?bbox=" + cacheReqsExt.minLon + "%2C" + cacheReqsExt.minLat + "%2C" + cacheReqsExt.maxLon + "%2C" + cacheReqsExt.maxLat + "&bboxSR=4326&layers=&layerDefs=&size=1024%2C1024&imageSR=4326&format=jpg&transparent=false&dpi=&time=&layerTimeOptions=&dynamicLayers=&gdbVersion=&mapScale=&rotation=&f=image"; //richTextBox1.Text += agsPayload + Environment.NewLine + Environment.NewLine; GetImgAsync(agsPayload, tempFileInfo.fileHandle); }
private static void HandleView(NetworkStream stream, FileRequestInfo requestInfo) { var context = new SmartShareContext(); var file = context.UploadedFiles.First(x => x.Name == requestInfo.FileName); if (!(file.Password == requestInfo.Password)) { return; } if (file.DownloadsExceeded() || file.IsExpired()) { stream.Close(); file.Delete(context); return; } var view = file.CreateView(); var responseSerializer = new XmlSerializer(typeof(FileView)); responseSerializer.Serialize(stream, view); }
static void HandleUpload(NetworkStream stream, FileRequestInfo requestInfo) { var context = new SmartShareContext(); var connection = context.Database.GetDbConnection(); // check if file already exists, and if that file is expired we can delete it and continue the upload. try { var possibleFile = context.UploadedFiles.First(x => x.Name == requestInfo.FileName); if (possibleFile != null) { if (possibleFile.DownloadsExceeded() || possibleFile.IsExpired()) { possibleFile.Delete(context); } else { return; } } } catch (InvalidOperationException e) { Console.WriteLine("File table is empty"); } stream.Write(new byte[1] { ACK_BYTE }); // let client know we're ready to receive if (connection is NpgsqlConnection) { var blobManager = new NpgsqlLargeObjectManager((NpgsqlConnection)connection); uint oid; connection.Open(); using (var transaction = connection.BeginTransaction()) { oid = blobManager.Create(); using (var blobStream = blobManager.OpenReadWrite(oid)) { stream.CopyTo(blobStream); } transaction.Commit(); } stream.Close(); connection.Close(); Console.WriteLine("Upload successful."); var uploadedFile = new UploadedFile(requestInfo, oid); context.Add(uploadedFile); context.SaveChanges(); } else { throw new NotSupportedException("Unsupported database adapter"); } }