Ejemplo n.º 1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            Contract.Requires(document != null);
            int bytesRead = 0;
            int remaining = (int)Math.Min(count, Length - documentOffset);

            while (remaining > 0)
            {
                if (segment == null || segmentOffset >= segment.Content.Length)
                {
                    if (segmentNumber < document.Segments.Count)
                    {
                        segment       = document.Segments.ElementAt(segmentNumber++);
                        segmentOffset = 0;
                    }
                    else
                    {
                        break;
                    }
                }
                int segmentCount = (int)Math.Min(remaining, segment.Content.Length - segmentOffset);
                Buffer.BlockCopy(segment.Content, segmentOffset, buffer, offset, segmentCount);
                segmentOffset += segmentCount;
                remaining     -= segmentCount;
                bytesRead     += segmentCount;
            }
            documentOffset += bytesRead;
            return(bytesRead);
        }
Ejemplo n.º 2
0
        public ActionResult UploadFiles()
        {
            try
            {
                if (Request.InputStream.Length != 0)
                {
                    string type       = Request.RequestType;
                    string fileName   = Request.QueryString["file"];
                    string parameters = Request.QueryString["param"];
                    int    requestId  = Int32.Parse(parameters.Substring(parameters.IndexOf('=') + 1));
                    bool   lastChunk  = string.IsNullOrEmpty(Request.QueryString["last"]) ? true : bool.Parse(Request.QueryString["last"]);
                    bool   firstChunk = string.IsNullOrEmpty(Request.QueryString["first"]) ? true : bool.Parse(Request.QueryString["first"]);
                    long   startByte  = string.IsNullOrEmpty(Request.QueryString["offset"]) ? 0 : long.Parse(Request.QueryString["offset"]);

                    ModularProgramDocument document = null;
                    if (firstChunk)
                    {
                        document           = Documents.All.FirstOrDefault(d => d.Name == fileName && d.RequestId == requestId) ?? Documents.Add(new ModularProgramDocument());
                        document.Name      = fileName;
                        document.RequestId = requestId;
                        document.Segments.ToList().ForEach(s => DocumentSegments.Remove(s));
                        document.MimeType = GetMimeType(fileName);
                    }
                    else
                    {
                        document = Documents.All.FirstOrDefault(d => d.Name == fileName && d.RequestId == requestId);
                    }

                    ModularProgramDocumentSegment segment = new ModularProgramDocumentSegment();
                    segment.Document = document;
                    document.Segments.Add(segment);
                    UnitOfWork.Commit();
                    long   offset = 0;
                    byte[] buffer = new byte[Math.Min(Request.InputStream.Length - Request.InputStream.Position, _buffer_size)];
                    var    ctx    = (UnitOfWork as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext;

                    int bytesRead = 0;
                    while ((bytesRead = Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        if (offset == 0)
                        {
                            ctx.ExecuteStoreCommand(@"update ModularProgramDocumentSegments set Content = convert( varbinary(max), {0} ) where Id = {1}", buffer, segment.Id);
                        }
                        else
                        {
                            ctx.ExecuteStoreCommand(@"update ModularProgramDocumentSegments set Content = Content + {0} where Id = {1}", buffer, segment.Id);
                        }
                        offset += bytesRead;
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e);
                throw e;
            }
            return(null);
        }