private int GetDownloadCount(string name)
 {
     if (dc != null)
     {
         return(dc.Count(name));
     }
     return(0);
 }
Esempio n. 2
0
        private void loadGrid()
        {
            List <Item> itemsList = new List <Item>();

            try
            {
                DirectoryInfo di    = new DirectoryInfo(Configuration.FilesPath);
                FileInfo[]    files = di.GetFiles("*.*", SearchOption.TopDirectoryOnly);

                foreach (FileInfo d in files)
                {
                    itemsList.Add(new Item(d.Name, dc.Count(d.Name)));
                }
            }
            catch (Exception)
            {
            }

            GridView1.DataSource = itemsList;
            GridView1.DataBind();
        }
Esempio n. 3
0
 protected void Page_Load(object sender, EventArgs e)
 {
     dc = new DownloadCount();
     loadGrid();
     lblDownloadsCount.Text = dc.Count().ToString();
 }
        public void ProcessRequest(HttpContext context)
        {
            DownloadCount dc       = new DownloadCount();
            const long    packSize = 20000;// 65536;
            string        fileName = context.Request.Url.Segments[context.Request.Url.Segments.Length - 1];
            FileStream    file;
            long          offset         = 0;
            long          rangeBegin     = 0;
            long          rangeEnd       = 0;
            long          dataTransfered = 0;
            long          fileSize       = 0;

            if (context.Session != null)
            {
                try
                {
                    context.Session.Add("ID", context.Session.SessionID);
                }
                catch (Exception)
                {
                    context.Session.Add("ID", dc.Count().ToString());
                }
            }

            if (File.Exists(Configuration.FilesPath + fileName))
            {
                context.Response.Clear();

                file = new FileStream(Configuration.FilesPath + fileName, FileMode.Open, FileAccess.Read);

                fileSize   = file.Length;
                rangeBegin = 0;
                rangeEnd   = file.Length;

                CalculateRange(context.Request, file.Length, ref rangeBegin, ref rangeEnd);

                //If isn't range
                if (rangeBegin == 0 && (rangeEnd == file.Length))
                {
                    context.Response.StatusCode = 200;
                }
                else
                {
                    context.Response.AppendHeader("Content-Range", "bytes " + rangeBegin + "-" + rangeEnd + "/" + file.Length.ToString());
                    context.Response.StatusCode = 206;
                }

                context.Response.AppendHeader("Content-Length", file.Length.ToString());
                context.Response.AppendHeader("Last-Modified", File.GetLastAccessTimeUtc(Configuration.FilesPath + fileName).ToString());
                context.Response.AppendHeader("Accept-Ranges", "bytes");
                context.Response.AppendHeader("ETag", "id_test");
                context.Response.ContentType = "application/octet-stream";

                if (!context.Request.HttpMethod.Equals("HEAD"))
                {
                    context.Response.Flush();

                    offset = rangeBegin;
                    int    readCount;
                    byte[] buffer = new Byte[packSize];
                    file.Seek(offset, SeekOrigin.Begin);
                    while (context.Response.IsClientConnected && offset < rangeEnd)
                    {
                        readCount = file.Read(buffer, 0, (int)Math.Min(rangeEnd - rangeBegin, buffer.Length));

                        context.Response.OutputStream.Write(buffer, 0, readCount);
                        context.Response.Flush();

                        offset         += readCount;
                        dataTransfered += readCount;
                    }
                }
                file.Dispose();
                file.Close();
            }
            else
            {
                Error.ErrorLog err = new Error.ErrorLog(new Exception("Filename = " + Configuration.FilesPath + fileName), context.Request);
                context.Response.StatusCode = 404;
            }

            dc.AddDownload(context, fileName, fileSize, dataTransfered, rangeBegin, rangeEnd);
            context.Response.End();
        }
Esempio n. 5
0
 protected void Page_Load(object sender, EventArgs e)
 {
     dc = new DownloadCount();
     loadGrid();
     lblDownloadsCount.Text = dc.Count().ToString();
 }
Esempio n. 6
0
        public void ProcessRequest(HttpContext context)
        {
            DownloadCount dc = new DownloadCount();
            const long packSize = 20000;// 65536;
            string fileName = context.Request.Url.Segments[context.Request.Url.Segments.Length - 1];
            FileStream file;
            long offset = 0;
            long rangeBegin = 0;
            long rangeEnd = 0;
            long dataTransfered = 0;
            long fileSize = 0;

            if (context.Session != null)
            {
                try
                {
                    context.Session.Add("ID", context.Session.SessionID);
                }
                catch (Exception)
                {
                    context.Session.Add("ID", dc.Count().ToString());
                }
            }

            if (File.Exists(Configuration.FilesPath + fileName))
            {
                context.Response.Clear();

                file = new FileStream(Configuration.FilesPath + fileName, FileMode.Open, FileAccess.Read);

                fileSize = file.Length;
                rangeBegin = 0;
                rangeEnd = file.Length;

                CalculateRange(context.Request, file.Length, ref rangeBegin, ref rangeEnd);

                //If isn't range
                if (rangeBegin == 0 && (rangeEnd == file.Length))
                {
                    context.Response.StatusCode = 200;
                }
                else
                {
                    context.Response.AppendHeader("Content-Range", "bytes " + rangeBegin + "-" + rangeEnd + "/" + file.Length.ToString());
                    context.Response.StatusCode = 206;
                }

                context.Response.AppendHeader("Content-Length", file.Length.ToString());
                context.Response.AppendHeader("Last-Modified", File.GetLastAccessTimeUtc(Configuration.FilesPath + fileName).ToString());
                context.Response.AppendHeader("Accept-Ranges", "bytes");
                context.Response.AppendHeader("ETag", "id_test");
                context.Response.ContentType = "application/octet-stream";

                if (!context.Request.HttpMethod.Equals("HEAD"))
                {
                    context.Response.Flush();

                    offset = rangeBegin;
                    int readCount;
                    byte[] buffer = new Byte[packSize];
                    file.Seek(offset, SeekOrigin.Begin);
                    while (context.Response.IsClientConnected && offset < rangeEnd)
                    {
                        readCount = file.Read(buffer, 0, (int)Math.Min(rangeEnd - rangeBegin, buffer.Length));

                        context.Response.OutputStream.Write(buffer, 0, readCount);
                        context.Response.Flush();

                        offset += readCount;
                        dataTransfered += readCount;
                    }
                }
                file.Dispose();
                file.Close();
            }
            else
            {
                Error.ErrorLog err = new Error.ErrorLog(new Exception("Filename = " + Configuration.FilesPath + fileName), context.Request);
                context.Response.StatusCode = 404;
            }

            dc.AddDownload(context, fileName, fileSize, dataTransfered, rangeBegin, rangeEnd);
            context.Response.End();
        }