Esempio n. 1
0
        /// <summary>
        /// Will send a file to client.
        /// </summary>
        /// <param name="context">HTTP context containing outbound stream.</param>
        /// <param name="response">Response containing headers.</param>
        /// <param name="stream">File stream</param>
        private void SendFile(IHttpContext context, Stream stream, string url)
        {
            var             worker  = new HTTPFileUploader(bufferService, uploadLimiter);
            TransferSession session = null;

            try
            {
                if (stream.Length > Model.FREE_FILE_LIMIT)
                {
                    session = new TransferSession(worker);
                    model.TransferSessions.Add(session);
                }

                //Try to find the username of the request
                string userName = context.RemoteEndPoint.Address.ToString();
                Node   search   =
                    model.Network.Nodes.ToList().Where(n => n.NodeType != ClientType.Overlord && n.Host == userName).
                    FirstOrDefault();
                if (null != search && !string.IsNullOrEmpty(search.Nickname))
                {
                    userName = search.Nickname;
                }

                worker.DoUpload(context, stream, userName, url);

                //Add log of the upload
                double seconds = (DateTime.Now - worker.TransferStart).TotalSeconds;
                var    txlog   = new TransferLog();
                txlog.Nickname  = userName;
                txlog.Completed = DateTime.Now;
                txlog.Filename  = Path.GetFileName(url);
                txlog.Path      = Path.GetDirectoryName(url);
                if (!string.IsNullOrEmpty(txlog.Path))
                {
                    txlog.Path = txlog.Path.Replace('\\', '/');
                    if (txlog.Path.StartsWith("/"))
                    {
                        txlog.Path = txlog.Path.Substring(1);
                    }
                }

                txlog.Size = worker.Length - worker.ResumePoint;
                if (txlog.Size < 0)
                {
                    txlog.Size = 0;
                }
                if (0 != seconds)
                {
                    txlog.Speed = (int)(txlog.Size / seconds);
                }
                model.CompletedUploads.Add(txlog);
            }
            finally
            {
                if (null != session)
                {
                    model.TransferSessions.Remove(session);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Will send a file to client.
        /// </summary>
        /// <param name="context">HTTP context containing outbound stream.</param>
        /// <param name="response">Response containing headers.</param>
        /// <param name="stream">File stream</param>
        private void SendFile(IHttpContext context, Stream stream, string url)
        {
            var worker = new HTTPFileUploader(bufferService, uploadLimiter);
            TransferSession session = null;
            try
            {
                if (stream.Length > Model.FREE_FILE_LIMIT)
                {
                    session = new TransferSession(worker);
                    model.TransferSessions.Add(session);
                }

                //Try to find the username of the request
                string userName = context.RemoteEndPoint.Address.ToString();
                Node search =
                    model.Network.Nodes.ToList().Where(n => n.NodeType != ClientType.Overlord && n.Host == userName).
                        FirstOrDefault();
                if (null != search && !string.IsNullOrEmpty(search.Nickname))
                    userName = search.Nickname;

                worker.DoUpload(context, stream, userName, url);

                //Add log of the upload
                double seconds = (DateTime.Now - worker.TransferStart).TotalSeconds;
                var txlog = new TransferLog();
                txlog.Nickname = userName;
                txlog.Completed = DateTime.Now;
                txlog.Filename = Path.GetFileName(url);
                txlog.Path = Path.GetDirectoryName(url);
                if (!string.IsNullOrEmpty(txlog.Path))
                {
                    txlog.Path = txlog.Path.Replace('\\', '/');
                    if (txlog.Path.StartsWith("/"))
                        txlog.Path = txlog.Path.Substring(1);
                }

                txlog.Size = worker.Length - worker.ResumePoint;
                if (txlog.Size < 0)
                    txlog.Size = 0;
                if (0 != seconds)
                    txlog.Speed = (int) (txlog.Size/seconds);
                model.CompletedUploads.Add(txlog);
            }
            finally
            {
                if (null != session)
                    model.TransferSessions.Remove(session);
            }
        }