public HttpResponseMessage Open(string path)
        {
            Log4Log.Info(Request.ToString());
            ContentLog.WriteLine(Request.ToString());

            var httpResponseMessage = Request.Response(path);

            Log4Log.Info(httpResponseMessage.ToString());
            ContentLog.WriteLine(httpResponseMessage.ToString());

            return(httpResponseMessage);
        }
        public HttpResponseMessage Index()
        {
            var path = Application.StartupPath;

            Log4Log.Info(Request.ToString());
            ContentLog.WriteLine(Request.ToString());

            var httpResponseMessage = Request.Response(path);

            Log4Log.Info(httpResponseMessage.ToString());
            ContentLog.WriteLine(httpResponseMessage.ToString());

            return(httpResponseMessage);
        }
        public HttpResponseMessage Download(string filePath)
        {
            Log4Log.Info(Request.ToString());
            ContentLog.WriteLine(Request.ToString());

            if (File.Exists(filePath) == false)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "文件未找到"));
            }
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            var fileName = Path.GetFileName(filePath);

            try
            {
                var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                {
                    response.Content = new StreamContent(fileStream);
                    response.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
                    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = fileName
                    };
                }

                Log4Log.Info(response.ToString());
                ContentLog.WriteLine(response.ToString());


                return(response);
            }
            catch (Exception exception)
            {
                var httpResponseMessage = Request.CreateErrorResponse(HttpStatusCode.NotFound, exception.Message);

                Log4Log.Info(httpResponseMessage.ToString());
                ContentLog.WriteLine(httpResponseMessage.ToString());

                return(httpResponseMessage);
            }
        }
示例#4
0
        public HttpResponseMessage Play(HttpRequestMessage request, string filePath)
        {
            Log4Log.Info(Request.ToString());
            ContentLog.WriteLine(Request.ToString());

            try
            {
                var assembly       = Assembly.GetExecutingAssembly();
                var resourceStream = assembly.GetManifestResourceStream("YYX.FileFinder.video.html");
                if (resourceStream == null)
                {
                    throw new Exception("页面异常");
                }
                var response = request.CreateResponse(HttpStatusCode.OK);
                using (var stream = new StreamReader(resourceStream))
                {
                    var html = stream.ReadToEnd();
                    var urlEncodeFilePath = HttpUtility.UrlEncode(filePath);
                    var videoLink         = $"/File/Download?filePath={urlEncodeFilePath}";
                    html = html.Replace("{videoLink}", videoLink);

                    response.Content = new StringContent(html, Encoding.UTF8);
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                }

                Log4Log.Info(response.ToString());
                ContentLog.WriteLine(response.ToString());

                return(response);
            }
            catch (Exception exception)
            {
                var httpResponseMessage = Request.CreateErrorResponse(HttpStatusCode.NotFound, exception.Message);

                Log4Log.Info(httpResponseMessage.ToString());
                ContentLog.WriteLine(httpResponseMessage.ToString());

                return(httpResponseMessage);
            }
        }