Exemple #1
0
        private bool HandleGet(RequestEventArgs e, NetworkRequest req)
        {
            //No url?
            if (string.IsNullOrEmpty(req.Param))
                return false;

            string[] possiblePaths;


            if (shareInfoService.ToLocalPath(req.Param, out possiblePaths))
            {
                foreach (string possiblePath in possiblePaths)
                {
                    if (File.Exists(possiblePath))
                    {
                        var ffu = new FAPFileUploader(bufferService, serverUploadLimiterService);
                        var session = new TransferSession(ffu);
                        model.TransferSessions.Add(session);
                        try
                        {
                            //Try to find the username of the request
                            string userName = e.Context.RemoteEndPoint.Address.ToString();
                            Node search = model.Network.Nodes.ToList().Where(n => n.ID == req.SourceID).FirstOrDefault();
                            if (null != search && !string.IsNullOrEmpty(search.Nickname))
                                userName = search.Nickname;

                            using (
                                FileStream fs = File.Open(possiblePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                ffu.DoUpload(e.Context, fs, userName, possiblePath);
                            }

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

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

            e.Response.Status = HttpStatusCode.NotFound;
            var generator = new ResponseWriter();
            generator.SendHeaders(e.Context, e.Response);
            return true;
        }
Exemple #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);
            }
        }