private Document GetView(Data.Document d, Guid reference) { return(new Document { Id = d.Id, Reference = reference, Title = d.FileName, Size = d.Size, Type = d.Type, Mime = d.MimeType, Url = Url.Route("defaultApi", new { controller = this.ControllerContext.ControllerDescriptor.ControllerName, action = "Get", id = d.Id }), Thumbnail = Url.Route("defaultApi", new { controller = this.ControllerContext.ControllerDescriptor.ControllerName, action = "Thumbnail", id = d.Id }) }); }
public static Document ProcessImage(System.IO.Stream source, IKcsarContext ctx, string filename, Guid trainingId) { Bitmap bitmap = (Bitmap)Image.FromStream(source); string tempFile = System.IO.Path.GetTempFileName(); bitmap.Save(tempFile, ImageFormat.Jpeg); byte[] contents = System.IO.File.ReadAllBytes(tempFile); System.IO.File.Delete(tempFile); bitmap.Dispose(); Document doc = new Document { Size = contents.Length, FileName = System.IO.Path.GetFileName(filename), MimeType = "image/jpeg", Contents = contents, ReferenceId = trainingId, Type = "unknown" }; ctx.Documents.Add(doc); return doc; }
private DocumentView GetView(Document d, Guid reference) { return new DocumentView { Id = d.Id, Reference = reference, Title = d.FileName, Size = d.Size, Type = d.Type, Mime = d.MimeType, Url = Url.Route("defaultApi", new { controller = this.ControllerContext.ControllerDescriptor.ControllerName, action = "Get", id = d.Id }), Thumbnail = Url.Route("defaultApi", new { controller = this.ControllerContext.ControllerDescriptor.ControllerName, action = "Thumbnail", id = d.Id }) }; }
public static Document[] ReceiveDocument(Stream contentStream, string filename, int length, IKcsarContext ctx, Guid reference, string type) { List<Document> docs = new List<Document>(); if (filename.ToLowerInvariant().EndsWith(".tif", StringComparison.OrdinalIgnoreCase)) { TiffBitmapDecoder decode = new TiffBitmapDecoder(contentStream, BitmapCreateOptions.None, BitmapCacheOption.None); int frameCount = decode.Frames.Count; for (int i = 0; i < frameCount; i++) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); JpegBitmapEncoder encode = new JpegBitmapEncoder(); encode.Frames.Add(BitmapFrame.Create(decode.Frames[i])); encode.Save(ms); ms.Seek(0, System.IO.SeekOrigin.Begin); docs.Add(Kcsara.Database.Web.Documents.ProcessImage(ms, ctx, filename.Replace(".tif", string.Format("-{0}.jpg", i)), reference)); ms.Dispose(); } } else if (filename.ToLowerInvariant().EndsWith(".jpg")) { docs.Add(Kcsara.Database.Web.Documents.ProcessImage(contentStream, ctx, filename, reference)); } else { byte[] contents = new byte[length]; contentStream.Read(contents, 0, length); Document doc = new Document { Size = length, FileName = System.IO.Path.GetFileName(filename), Contents = contents, ReferenceId = reference, MimeType = GuessMime(filename), Type = type }; ctx.Documents.Add(doc); docs.Add(doc); } return docs.ToArray(); }
/// <summary> /// /// </summary> /// <param name="d"></param> private void SaveDocumentFile(Document d) { if (string.IsNullOrWhiteSpace(d.StorePath)) { string path = string.Empty; for (int i = 0; i < Document.StorageTreeDepth; i++) { path += ((i > 0) ? "\\" : "") + rand.Next(Document.StorageTreeSpan).ToString(); } if (!System.IO.Directory.Exists(Document.StorageRoot + path)) { System.IO.Directory.CreateDirectory(Document.StorageRoot + path); } path += "\\" + d.Id.ToString(); d.StorePath = path; } System.IO.File.WriteAllBytes(Document.StorageRoot + d.StorePath, d.Contents); }