示例#1
0
        /// <summary>
        /// Reads bytes for a specific document from the database.
        /// </summary>
        /// <param name="dataContext">The DataContext for the database.</param>
        /// <param name="documentID">The ID of the document to read the content for.</param>
        /// <param name="offset">The starting offset to read the content bytes from.</param>
        /// <param name="length">The length of content bytes to read.</param>
        /// <returns>A byte array containing the document content for the specified byte range.</returns>
        public async Task <byte[]> ReadChunk(Data.DataContext dataContext, Guid documentID, int offset, int length)
        {
            string filename = Path.Combine(_uploadPath, documentID.ToString("D") + ".part");

            if (!File.Exists(filename))
            {
                lock (_lock)
                {
                    if (!File.Exists(filename))
                    {
                        string tempFileName = Path.Combine(_uploadPath, "temp_" + documentID.ToString("D") + ".part");
                        using (var fs = new FileStream(tempFileName, FileMode.Append, FileAccess.Write, FileShare.Write))
                            using (var dbStream = new Data.Documents.DocumentStream(dataContext, documentID))
                            {
                                dbStream.CopyTo(fs);
                                fs.Flush();
                                fs.Close();
                            }

                        File.Move(tempFileName, filename);
                    }
                }
            }


            byte[] buffer = new byte[length];
            using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                stream.Seek(offset, SeekOrigin.Begin);
                await stream.ReadAsync(buffer, 0, buffer.Length);
            }

            return(buffer);
        }
        /// <summary>
        /// Returns a readonly stream for the specified document from the local disk cache.
        /// </summary>
        /// <param name="dataContext">The current datacontext to use if the cache needs to be populated.</param>
        /// <param name="documentID">The ID of the document.</param>
        /// <returns></returns>
        public Stream GetStream(Data.DataContext dataContext, Guid documentID)
        {
            string filename = Path.Combine(_uploadPath, documentID.ToString("D") + ".part");

            if (!File.Exists(filename))
            {
                lock (_lock)
                {
                    if (!File.Exists(filename))
                    {
                        string tempFileName = Path.Combine(_uploadPath, "temp_" + documentID.ToString("D") + ".part");
                        using (var fs = new FileStream(tempFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                            using (var dbStream = new Data.Documents.DocumentStream(dataContext, documentID))
                            {
                                dbStream.CopyTo(fs);
                                fs.Flush();
                                fs.Close();
                            }

                        File.Move(tempFileName, filename);
                    }
                }
            }

            return(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
        }
        public void DownloadDocument()
        {
            Guid documentID = new Guid("6F1D10BB-B3E2-4028-A1E5-A84D011E4249");

            using (var db = new DataContext())
            {
                var document = db.Documents.Single(d => d.ID == documentID);

                using (var writer = File.OpenWrite(document.FileName))
                    using (var reader = new Data.Documents.DocumentStream(db, documentID))
                    {
                        reader.CopyTo(writer);
                        reader.Flush();
                        writer.Flush();
                        writer.Close();
                    }
            }
        }