public UploadResponse Upload(UploadRequest request) { var tempFile = Path.GetTempFileName(); try { // write data to a temp file File.WriteAllBytes(tempFile, request.DataContent); // create the new document object, and put the remote file var args = new AttachedDocumentCreationArgs { MimeType = request.MimeType, FileExtension = request.FileExtension, LocalContentFilePath = tempFile }; var document = AttachedDocument.Create(args, AttachmentStore.GetClient()); PersistenceContext.Lock(document, DirtyState.New); PersistenceContext.SynchState(); var assembler = new AttachedDocumentAssembler(); return(new UploadResponse(assembler.CreateAttachedDocumentSummary(document))); } finally { File.Delete(tempFile); } }
public HttpResponseMessage GetConversationFile(string conversationID, string fileID) { ChatSession session = Verifier.SessionFromToken(Request); if (session == null) { throw new UnauthorizedAccessException(); } Guid conversationGid = Guid.Parse(conversationID); if (!session.Owner.ConversationID.Contains(conversationGid)) { throw new UnauthorizedAccessException(); } if (!File.Exists(Path.Combine(String.Format(SavePath, conversationID), fileID))) { throw new FileNotFoundException(); } String filePath = Path.Combine(String.Format(SavePath, conversationID), fileID); String fileName = AttachmentStore.Parse(Guid.Parse(fileID)); FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); new CustomContentTypeProvider().TryGetContentType(fileName, out var contentType); if (contentType == null || contentType.Length < 1) { contentType = "application/octet-stream"; } if (Request.Headers.Range != null) { HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent); partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, contentType, ServerSettings.BUFFER_SIZE); partialResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; return(partialResponse); } else { HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK); fullResponse.Content = new StreamContent(stream, ServerSettings.BUFFER_SIZE); fullResponse.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); fullResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; return(fullResponse); } }
public HttpResponseMessage GetAvatar(string userID) { ChatSession session = Verifier.SessionFromToken(Request); if (session == null) { throw new UnauthorizedAccessException(); } String filePath = Path.Combine(AvatarPath, userID); String fileName; if (!File.Exists(Path.Combine(AvatarPath, userID))) { if (session.Owner.Gender == Entity.EntityProperty.Gender.Female) { filePath = "Resource/Default/Avatar_Female.jpg"; } else { filePath = "Resource/Default/Avatar_Male.jpg"; } fileName = "Avatar.jpg"; } else { fileName = AttachmentStore.Parse(Guid.Parse(userID)); } FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); new CustomContentTypeProvider().TryGetContentType(fileName, out var contentType); if (contentType == null || contentType.Length < 1) { contentType = "application/octet-stream"; } if (Request.Headers.Range != null) { HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent); partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, contentType, ServerSettings.BUFFER_SIZE); return(partialResponse); } else { HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK); fullResponse.Content = new StreamContent(stream, ServerSettings.BUFFER_SIZE); fullResponse.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); return(fullResponse); } }
/// <summary> /// Initalizes the database using the connection string provided in app.config, gets /// employee collection and attachment store /// </summary> private void InitializeDatabaseAndCollections() { string conectionString = ConfigurationManager.AppSettings["ConnectionString"]; string collection = ConfigurationManager.AppSettings["CollectionName"]; _database = Client.NosDBClient.GetDatabase(conectionString); // Get instance of collection from the database _employeeCollection = _database.GetDBCollection <Employee>(collection); // Get an instance of the Attachment store from the database that has enabled attachments: _attachmentStore = _database.GetAttachmentStore(); }
public void AvatarUpload() { ChatSession session = Verifier.SessionFromToken(Request); if (session == null) { throw new UnauthorizedAccessException(); } if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } String fileName; Guid id = session.Owner.ID; Directory.CreateDirectory(AvatarPath); var streamProvider = new MultipartFormDataStreamProvider(AvatarPath); Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(p => { foreach (MultipartFileData fileData in streamProvider.FileData) { if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName)) { continue; } fileName = fileData.Headers.ContentDisposition.FileName; if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } AttachmentStore.Map(id, fileName); if (File.Exists(Path.Combine(AvatarPath, id.ToString()))) { File.Delete(Path.Combine(AvatarPath, id.ToString())); } File.Move(fileData.LocalFileName, Path.Combine(AvatarPath, id.ToString())); } }).Wait(); }
public static Dictionary <String, String> Upload(HttpRequestMessage Request, String SavePath, bool createThumb = false) { Dictionary <String, String> result = new Dictionary <String, String>(); String fileName; Guid id; Directory.CreateDirectory(SavePath); var streamProvider = new MultipartFormDataStreamProvider(SavePath); Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(p => { foreach (MultipartFileData fileData in streamProvider.FileData) { if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName)) { continue; } //if (new FileInfo(fileData.LocalFileName).Length > ServerSettings.MAX_SIZE_UPLOAD) //{ // File.Delete(fileData.LocalFileName); // continue; //} fileName = fileData.Headers.ContentDisposition.FileName; if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } id = Guid.NewGuid(); AttachmentStore.Map(id, fileName); result.Add(fileName, id.ToString()); File.Move(fileData.LocalFileName, Path.Combine(SavePath, id.ToString())); if (createThumb) { if (VideoExtensions.Contains(Path.GetExtension(fileName), StringComparer.OrdinalIgnoreCase)) { var ffProbe = new FFProbe(); var videoInfo = ffProbe.GetMediaInfo(Path.Combine(SavePath, id.ToString())); FFMpegConverter converter = new FFMpegConverter(); converter.GetVideoThumbnail(Path.Combine(SavePath, id.ToString()), Path.Combine(SavePath, id.ToString() + "_thumb.jpg"), (float)videoInfo.Duration.TotalSeconds / 2); } if (ImageExtensions.Contains(Path.GetExtension(fileName), StringComparer.OrdinalIgnoreCase)) { Image image = Image.FromFile(Path.Combine(SavePath, id.ToString())); int w = image.Width, h = image.Height; if (image.Height > 300) { h = 300; w = image.Width * 300 / image.Height; } else if (image.Width > 500) { w = 500; h = image.Height * 500 / image.Width; } image.GetThumbnailImage(w, h, null, IntPtr.Zero).Save(Path.Combine(SavePath, id.ToString() + "_thumb.jpg")); image.Dispose(); } } } }).Wait(); return(result); }