Пример #1
0
        public override string Run(string strFileName)
        {
            int     len = FileLength.ToString().Length;
            decimal cnt = Start + Step * RunCount++;

            return(base.Run(IsSamp ? cnt.ToString().PadLeft(len, '0') : cnt.ToString()));
        }
        public void ToString_Test(long length, string format, string expectedValue)
        {
            var fileLength = new FileLength(length);
            var actual     = fileLength.ToString(format, CultureInfo.InvariantCulture);

            Assert.AreEqual(expectedValue, actual);
        }
Пример #3
0
        //возвращает ассоциативный массив (словарь) со всеми вычисленными настройками
        public Dictionary <String, String> GetAllInfo()
        {
            Dictionary <String, String> info = new Dictionary <string, string>();

            info.Add("Имя файла", FileName);
            info.Add("Путь до файла", Path);
            info.Add("Длина файла", FileLength.ToString());
            info.Add("Дата создания файла", FileCreatedDate.ToString());
            info.Add("Состояние файла", CheckState());

            return(info);
        }
Пример #4
0
        public List <KeyValuePair <string, string> > ToKeyValuePairs()
        {
            var result = new List <KeyValuePair <string, string> >(10);

            // leave out null values, but string.Empty is preserved
            if (FileName != null)
            {
                result.Add("FileName", FileName);
            }

            if (ContentId > 0)
            {
                result.Add("ContentId", ContentId.ToString());
            }

            if (ContentType != null)
            {
                result.Add("ContentType", ContentType);
            }
            if (PropertyName != null)
            {
                result.Add("PropertyName", PropertyName);
            }

            result.Add("UseChunk", UseChunk.ToString());
            result.Add("Overwrite", Overwrite.ToString());
            result.Add("FileLength", FileLength.ToString());

            if (ChunkToken != null)
            {
                result.Add("ChunkToken", ChunkToken);
            }

            if (FileText != null)
            {
                result.Add("FileText", FileText);
            }

            return(result);
        }
Пример #5
0
        private int GetContentLength(string boundary)
        {
            int contentLength = 0;

            for (int i = 0; i < RangesStartIndexes.Length; i++)
            {
                contentLength += Convert.ToInt32(RangesEndIndexes[i] - RangesStartIndexes[i]) + 1;

                if (MultipartRequest)
                {
                    contentLength += boundary.Length + ContentType.Length + RangesStartIndexes[i].ToString().Length + RangesEndIndexes[i].ToString().Length + FileLength.ToString().Length + 49;
                }
            }

            if (MultipartRequest)
            {
                contentLength += boundary.Length + 4;
            }

            return(contentLength);
        }
Пример #6
0
        /// <summary>
        /// Enables processing of the result of an action method by a custom type that inherits from the ActionResult class. (Overrides ActionResult.ExecuteResult(ControllerContext).)
        /// </summary>
        /// <param name="context">The context within which the result is executed.</param>
        public override void ExecuteResult(ControllerContext context)
        {
            EntityTag = GenerateEntityTag(context);
            GetRanges(context.HttpContext.Request);

            if (ValidateRanges(context.HttpContext.Response) && ValidateModificationDate(context.HttpContext.Request, context.HttpContext.Response) && ValidateEntityTag(context.HttpContext.Request, context.HttpContext.Response))
            {
                context.HttpContext.Response.AddHeader("Last-Modified", HttpModificationDate.ToString("r"));
                context.HttpContext.Response.AddHeader("ETag", String.Format("\"{0}\"", EntityTag));
                context.HttpContext.Response.AddHeader("Accept-Ranges", "bytes");

                if (!RangeRequest)
                {
                    context.HttpContext.Response.AddHeader("Content-Length", FileLength.ToString());
                    context.HttpContext.Response.ContentType = ContentType;
                    context.HttpContext.Response.StatusCode  = 200;
                    if (!context.HttpContext.Request.HttpMethod.Equals("HEAD"))
                    {
                        WriteEntireEntity(context.HttpContext.Response);
                    }
                }
                else
                {
                    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

                    context.HttpContext.Response.AddHeader("Content-Length", GetContentLength(boundary).ToString());
                    if (!MultipartRequest)
                    {
                        context.HttpContext.Response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", RangesStartIndexes[0], RangesEndIndexes[0], FileLength));
                        context.HttpContext.Response.ContentType = ContentType;
                    }
                    else
                    {
                        context.HttpContext.Response.ContentType = String.Format("multipart/byteranges; boundary={0}", boundary);
                    }
                    context.HttpContext.Response.StatusCode = 206;
                    if (!context.HttpContext.Request.HttpMethod.Equals("HEAD"))
                    {
                        for (int i = 0; i < RangesStartIndexes.Length; i++)
                        {
                            if (MultipartRequest)
                            {
                                context.HttpContext.Response.Write(String.Format("--{0}\r\n", boundary));
                                context.HttpContext.Response.Write(String.Format("Content-Type: {0}\r\n", ContentType));
                                context.HttpContext.Response.Write(String.Format("Content-Range: bytes {0}-{1}/{2}\r\n\r\n", RangesStartIndexes[i], RangesEndIndexes[i], FileLength));
                            }

                            if (context.HttpContext.Response.IsClientConnected)
                            {
                                WriteEntityRange(context.HttpContext.Response, RangesStartIndexes[i], RangesEndIndexes[i]);
                                if (MultipartRequest)
                                {
                                    context.HttpContext.Response.Write("\r\n");
                                }
                            }
                            else
                            {
                                return;
                            }
                        }
                        if (MultipartRequest)
                        {
                            context.HttpContext.Response.Write(String.Format("--{0}--", boundary));
                        }
                    }
                }
            }
        }
Пример #7
0
        public void ManageMultipartDownload(/*Byte[] content, String name, HttpRequest request, HttpResponse response*/)
        {
            //Request = request;
            //Response = response;
            //FileName = name;
            ////if (FileContent == null)
            ////    FileContent = File.ReadAllBytes(FilePath);
            //FileLength = content.Length;
            //FileContent = content;
            //ContentType = "application/pdf";
            //Generate entity tag
            EntityTag = GenerateEntityTag();
            //Get ranges from request
            GetRanges(Request);

            //If all validations are successful
            if (ChangeSignature || Validate(Request, Response))
            {
                //Set common headers
                //Response.Buffer = false;
                Response.AddHeader("Last-Modified", FileModificationDate.ToString("r"));
                Response.AddHeader("ETag", String.Format("\"{0}\"", EntityTag));
                //Response.Cache.SetCacheability(HttpCacheability.Public); //required for etag output
                //Response.Cache.SetETag(EntityTag);
                Response.AddHeader("Accept-Ranges", "bytes");
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", "inline;filename=" + FileName);
                //If this is not range Request
                if (!RangeRequest)
                {
                    //Set standard headers
                    Response.AddHeader("Content-Length", FileLength.ToString());
                    //Response.AddHeader("Content-Type", "application/pdf");
                    //Set status code
                    Response.StatusCode = 200;

                    //If this is not HEAD Request
                    if (!Request.HttpMethod.Equals("HEAD"))
                    {
                        //Write entire file to Response
                        Console.WriteLine("Request.HttpMethod : " + Request.HttpMethod);
                        Console.WriteLine("Write entire file to Response");
                        WriteEntireEntity(Response);
                    }
                }
                //If this is range Request
                else
                {
                    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

                    //Compute and set content length
                    Response.AddHeader("Content-Length", GetContentLength(boundary).ToString());

                    //If this is not multipart Request
                    if (!MultipartRequest)
                    {
                        //Set content range and type
                        Response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", RangesStartIndexes[0], RangesEndIndexes[0], FileLength));
                        Response.ContentType = ContentType;
                    }
                    //Otherwise
                    else
                    {
                        //Set proper content type
                        Response.ContentType = String.Format("multipart/byteranges; boundary={0}", boundary);
                    }

                    //Set status code
                    Response.StatusCode = 206;

                    //If this not HEAD Request
                    if (!Request.HttpMethod.Equals("HEAD"))
                    {
                        //For each Requested range
                        for (int i = 0; i < RangesStartIndexes.Length; i++)
                        {
                            //If this is multipart Request
                            if (MultipartRequest)
                            {
                                //Write additional multipart info
                                Response.Write(String.Format("--{0}\r\n", boundary));
                                Response.Write(String.Format("Content-Type: {0}\r\n", ContentType));
                                Response.Write(String.Format("Content-Range: bytes {0}-{1}/{2}\r\n\r\n", RangesStartIndexes[i], RangesEndIndexes[i], FileLength));
                            }

                            //Write range (with multipart separator if required)
                            if (Response.IsClientConnected)
                            {
                                Console.Write("WriteEntityRange");
                                WriteEntityRange(Response, RangesStartIndexes[i], RangesEndIndexes[i]);
                                if (MultipartRequest)
                                {
                                    Response.Write("\r\n");
                                }
                                Response.Flush();
                            }
                            else
                            {
                                return;
                            }
                        }

                        //If this is multipart Request
                        if (MultipartRequest)
                        {
                            Response.Write(String.Format("--{0}--", boundary));
                        }
                    }
                }
            }
        }
Пример #8
0
 protected string GenerateEntityTag()
 {
     //Generate entity tag based on file name and length
     byte[] entityTagBytes = Encoding.ASCII.GetBytes(String.Format("{0}|{1}", FileName, FileLength.ToString()));
     return("W/" + Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(entityTagBytes)));
 }