public void OnDeleteFile(string name) { if (Directory.Exists(BaseDir + _currentDirectory)) { FileInfo file = new FileInfo(BaseDir + _currentDirectory + "/" + name); if (file.Exists) { try { Directory.Delete(file.FullName); _connection.Send(FileServerHelper.DeleteDirectoryResult(0, file.FullName)); LogService.Info(this, "delete file {0} success", name); } catch (Exception err) { LogService.ErrorE(this, string.Format("failed to delete file {0} under {1}", name, _currentDirectory), err); _connection.Send(FileServerHelper.DeleteDirectoryResult(-1, err.Message)); } return; } } LogService.Warn(this, "Directory {0} does not exist", name); _connection.Send(FileServerHelper.DeleteDirectoryResult(-2, name)); }
public void OnRequestFileSend(string file, int uploadToken, int size) { if (size > MaxFileUploadSize) { _connection.Send(FileServerHelper.FileSendResponse(0, -2)); } else if (/*Service.ValidateUploadToken(uploadToken)*/ true) { _fileAssembler = FileAssembler.CreateAssembler(BaseDir + "/" + _currentDirectory + "/" + file); LogService.Info(this, "Begin receiving file: {0}", _currentDirectory + "/" + file); if (_fileAssembler != null) { _connection.Send(FileServerHelper.FileSendResponse(0, size)); _toUploadSize = size; } else { _connection.Send(FileServerHelper.FileSendResponse(0, -2)); } } else { _connection.Send(FileServerHelper.FileSendResponse(0, -1)); } }
public ActionResult Delete(int id, documento documento) { try { documento = DocumentoRepository.GetOne(id); if (ExistDependences(documento)) { return(View(documento)); } if (!FileServerHelper.DeleteFile(documento.path)) { throw new Exception("Não foi possível salvar o arquivo"); } else { DocumentoRepository.Delete(documento); } return(RedirectToAction("List", new { message = "Dados excluídos com sucesso!" })); } catch { return(View()); } }
public FileStreamResult Download(int id) { documento anexos = DocumentoRepository.GetOne(id); string contentType = FileServerHelper.GetContentType(anexos.path.Substring(anexos.path.LastIndexOf('.'))); return(File(new FileStream(anexos.path, FileMode.Open), contentType)); }
public void OnListDirectorys(string name) { StringSequence strings = new StringSequence(); if (Directory.Exists(BaseDir + _currentDirectory)) { try { string[] dirs = Directory.GetDirectories(BaseDir + _currentDirectory + "/" + name); LogService.Info(this, "List folders under {0} success", name); for (int i = 0; i < dirs.Length; i++) { strings.GetElements().AddRange(dirs); } _connection.Send(FileServerHelper.ListDirectorysResult(0, strings)); return; } catch (Exception err) { LogService.ErrorE(this, string.Format("failed to list directory {0} under {1}", name, _currentDirectory), err); strings.Add(err.Message); _connection.Send(FileServerHelper.ListDirectorysResult(-1, strings)); } } else { LogService.Warn(this, "Directory {0} does not exist", name); _connection.Send(FileServerHelper.ListDirectorysResult(-2, strings)); } }
public ActionResult Create(documento documento) { LoadFormJuri(); try { if (validate(documento)) { return(View(documento)); } if (UploadFile(ref documento)) { DocumentoRepository.Create(documento); return(RedirectToAction("List", new { message = "Dados cadastrados com sucesso!" })); } else { throw new Exception("Ocorreu um erro ao criar o registro"); } } catch { if (!FileServerHelper.DeleteFile(documento.path)) { throw new Exception("Não foi possível excluir o arquivo"); } else { return(View(documento)); } } }
public void OnSetWorkingDirectory(string name) { if (Directory.Exists(BaseDir + "/" + name)) { _currentDirectory = name; _connection.Send(FileServerHelper.SetWorkingDirectoryResult(0, name)); } else { LogService.Warn(this, "Directory {0} does not exist", name); _connection.Send(FileServerHelper.SetWorkingDirectoryResult(-1, name)); } }
static void Main(string[] args) { MySqlConnectPoolHelper.getPool().InitMySqlConnectPool(); UserManagerHelper.InitUserManager(); CompanyManagerHelper.Initialize(); CompanyManagerHelper.StartAlarmStoreService(); CompanyManagerHelper.StartDataStoreService(); FileServerHelper.Initialize(); //CompanyManagerHelper.StartDeviceStatusThread(); //跨域配置 // config.EnableCors(new EnableCorsAttribute("*", "*", "*")); //var _client = RedisManager.GetClient(); //routeTemplate: "api/{controller}/{id}", LoggerManager.Log.Info("服务启动开始...\n"); try { Assembly.Load("IotCloudService.IotWebServerWebApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); HttpSelfHostConfiguration configuration = new HttpSelfHostConfiguration("http://127.0.0.1:8081"); using (HttpSelfHostServer httpServer = new HttpSelfHostServer(configuration)) { httpServer.Configuration.EnableCors(); httpServer.Configuration.MapHttpAttributeRoutes(); httpServer.Configuration.Filters.Add(new ApiSecurityFilter()); httpServer.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional }); httpServer.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } } catch (Exception ex) { LoggerManager.Log.Info(ex.Message); LoggerManager.Log.Info("出现异常,服务退出!\n"); Console.ReadLine(); } }
public void OnRequestFileBlock(string file, int blockId, int size, ref com.ideadynamo.foundation.buffer.ByteArray stream, int special) { if (_fileAssembler != null) { int thisBlockSize = stream.Buffer.Length - ByteArray.HEADERLENGTH; _fileAssembler.Append(blockId, stream.Buffer, ByteArray.HEADERLENGTH, thisBlockSize); _connection.Send(FileServerHelper.FileBlockResponse(0, size)); _toUploadSize -= thisBlockSize; if (special != 0 || _toUploadSize <= 0) { LogService.Info(this, "Receive file {0} success, total size {1}", file, _fileAssembler.Size); _fileAssembler.Dispose(); _fileAssembler = null; } } }
public async Task <FileResult> UploadSingleFile(String CompanyCode, String DeviceCode, String UserPhone) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var streamProvider = new RenamingMultipartFormDataStreamProvider(FileServerHelper.GetUploadFileRoot(), CompanyCode, DeviceCode, UserPhone); await Request.Content.ReadAsMultipartAsync(streamProvider); return(new FileResult { FileNames = streamProvider.FileData.Select(entry => entry.LocalFileName) }); }
public HttpResponseMessage DownloadFile(string fileName) { HttpResponseMessage result = null; String filePath = FileServerHelper.GetUploadFileRoot() + "123456"; FileStream fs = new FileStream(filePath, FileMode.Open); result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(fs); result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = "C:\\Temp\\123456.jpg"; return(result); }
public bool HandleProtocol(com.ideadynamo.foundation.buffer.ByteArray buffer) { if (_fileReceiveSession != null) { if (FileServerHelper.IntepretMessage(buffer, _fileReceiveSession)) { return(true); } } if (_fileSendSession != null) { if (FileClientHelper.IntepretMessage(buffer, _fileSendSession)) { return(true); } } return(false); }
private bool UploadFile(ref documento documento) { if (Request.Files != null && Request.Files[0].ContentLength > 0) { string path = String.Empty; if (!FileServerHelper.UploadFile(Request.Files[0].InputStream, Request.Files[0].FileName, ref path)) { throw new Exception("Não foi possível salvar o arquivo"); } else { documento.path = path; return(true); } } else { throw new Exception("O arquivo informado está vazio"); } }
public ActionResult Delete(string path, foto foto) { try { foto = FotoRepository.GetFotoById(path); if (!FileServerHelper.DeleteFile(Server.MapPath(foto.path))) { throw new Exception("Não foi possível salvar o arquivo"); } else { FotoRepository.Delete(foto); } return(RedirectToAction("List", new { message = "Dados excluídos com sucesso!" })); } catch { return(View()); } }
/*public void OnRequetFileDownload(string file, int uploadToken) * { * if (File.Exists(file)) * { * try * { * using (FileStream stream = new FileStream(file, FileMode.Open)) * { * int size = stream.Read(_buffer.Buffer, ByteArray.HEADERLENGTH, 1024 * 16 - ByteArray.HEADERLENGTH); * _sentSize = size; * * _buffer.Reset(); * _buffer.AdjustWriter(ByteArray.HEADERLENGTH + size); * _buffer.RefreshHeader(); * _connection.Send(FileServerHelper.DownFileBlockResponse(file, 0, size, _buffer, 0)); * } * } * catch (Exception err) * { * LogService.ErrorE(this, "Failed to start file downloading", err); * _connection.Send(FileServerHelper.FileDownloadResponse(file, -2)); * } * } * else * { * _connection.Send(FileServerHelper.FileDownloadResponse(file, -1)); * } * } * * public void OnFileBlockResponse(int file, int blockId, int blockSize, int receivedSize, int expectSize) * { * throw new NotImplementedException(); * } * * public void OnDownloadFinishResponse(int file, int totalSize, string md5) * { * throw new NotImplementedException(); * } */ public void OnCreateDirectory(string name) { if (Directory.Exists(BaseDir + _currentDirectory)) { try { Directory.CreateDirectory(BaseDir + _currentDirectory + "/" + name); LogService.Info(this, "Create folder {0} under {1} success", name, _currentDirectory); _connection.Send(FileServerHelper.CreateDirectoryResult(0, name)); } catch (Exception err) { LogService.ErrorE(this, string.Format("failed to create directory {0} under {1}", name, _currentDirectory), err); _connection.Send(FileServerHelper.CreateDirectoryResult(-1, err.Message)); } } else { LogService.Warn(this, "Directory {0} does not exist", name); _connection.Send(FileServerHelper.CreateDirectoryResult(-2, name)); } }
public void GetDirectory(string dir, string localdir) { _connection.Send(FileServerHelper.RequestFileDownload(dir, localdir, true)); }
public void GetFile(string file, string localfile) { _connection.Send(FileServerHelper.RequestFileDownload(file, localfile, false)); }