/// <summary> /// Uploads a new file /// <para>Podio API Reference: https://developers.podio.com/doc/files/upload-file-1004361 </para> /// </summary> /// <param name="filePath">Full physical path to the file</param> /// <param name="fileName">File Name with extension</param> /// <returns></returns> public async Task <FileAttachment> UploadFile(string filePath, string fileName) { if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { byte[] data = await FileUtils.ReadAllBytesAsync(filePath); string mimeType = MimeTypeMapping.GetMimeType(Path.GetExtension(filePath)); return(await UploadFile(fileName, data, mimeType)); } else { throw new FileNotFoundException("File not found in the specified path"); } }
public async Task <HttpResponseMessage> Get([FromUri] AvatarRequest avatarRequest) { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Bad Request")); } try { return(await Task <HttpResponseMessage> .Factory.StartNew(() => { NeeoFileInfo fileInfo = null; ulong avatarUpdatedTimeStamp = 0; int dimension = Convert.ToInt32(avatarRequest.Dimension); NeeoUser user = new NeeoUser(avatarRequest.Uid); switch (user.GetAvatarState(avatarRequest.Timestamp, false, out avatarUpdatedTimeStamp, out fileInfo)) { case AvatarState.Modified: var response = Request.CreateResponse(HttpStatusCode.OK); response.Headers.Add("ts", avatarUpdatedTimeStamp.ToString()); response.Content = new ByteArrayContent(MediaUtility.ResizeImage(fileInfo.FullPath, dimension, dimension)); //response.Content = new StreamContent(new FileStream(fileInfo.FullPath,FileMode.Open)); response.Content.Headers.ContentType = new MediaTypeHeaderValue( MimeTypeMapping.GetMimeType(fileInfo.Extension).GetDescription()); return response; case AvatarState.NotModified: return Request.CreateResponse(HttpStatusCode.NotModified); default: return Request.CreateResponse(HttpStatusCode.BadRequest); } })); } catch (AggregateException aggregateException) { Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, aggregateException.Message, aggregateException, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name); } catch (Exception exception) { Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, exception.Message, exception, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name); } return(Request.CreateResponse(HttpStatusCode.InternalServerError)); }
public HttpResponseMessage Get([FromUri] FileRequest fileRequest) { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Bad Request")); } if (NeeoUtility.GenerateSignature(fileRequest.Name + fileRequest.FileCategory.ToString("D") + fileRequest.MediaType.ToString("D")) == fileRequest.Signature) { byte[] fileBytes; var file = GetRequestedFile(fileRequest); if (file != null) { if (Request.Headers.Range != null) { var delimeter = new char[] { '-', '=' }; var rangeValues = Request.Headers.Range.ToString().Split(delimeter); if (rangeValues.Length != 3) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } if (!((Convert.ToInt64(rangeValues[1]) <= Convert.ToInt64(rangeValues[2])) && (Convert.ToInt64(rangeValues[2]) <= file.Info.Length))) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } fileBytes = File.GetBytesArray(file.Info.FullPath, Convert.ToInt32(rangeValues[1]), Convert.ToInt32(rangeValues[2])); } else { fileBytes = File.GetBytesArray(file.Info.FullPath); } var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new ByteArrayContent(fileBytes); //new StreamContent(File.GetStream(file.Info.FullPath)); //response.Content = new StreamContent(new FileStream(fileInfo.FullPath,FileMode.Open)); response.Content.Headers.ContentType = new MediaTypeHeaderValue( MimeTypeMapping.GetMimeType(file.Info.Extension).GetDescription()); return(response); } Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "File ID: " + fileRequest.FullName + ", File Category: Shared, Status: File does not exists."); } return(Request.CreateResponse(HttpStatusCode.BadRequest, "Bad Request")); }
public FileContent GetFileAttachment(Guid fileId) { var result = fileAttachmentRepository.GetAll().Where(t => t.Id == fileId) .Select(t => new FileContent() { Id = t.Id, AttachmentType = (FileAttachmentType)t.AttachmentTypeValue, Extension = t.Extension, FileName = t.FileName, Path = t.Path, PhysicalName = t.PhysicalName, RelatedId = t.RelatedId, }).FirstOrDefault(); if (result == null) { return(null); } var uploadPath = AppSetting.Get <string>("FileUploadPath"); if (!string.IsNullOrWhiteSpace(result.Path)) { if (result.Path != "upload") { uploadPath = Path.Combine(uploadPath, result.Path); } } var filePath = Path.Combine(uploadPath, result.PhysicalName); result.MimeType = MimeTypeMapping.GetMimeType(result.Extension ?? ""); if (!System.IO.File.Exists(filePath)) { throw new ResourceNotFoundException(); } var stream = System.IO.File.OpenRead(filePath); result.FileStream = stream; return(result); }