Example #1
0
        public void HandleGet(HttpListenerRequest request, HttpListenerResponse response)
        {
            string queryString = request.Url.Query;
            var    queryParts  = Server.ParseQueryString(queryString);

            string presetName = queryParts.GetFirstValue("preset");

            if (string.IsNullOrEmpty(presetName))
            {
                response.StatusCode = 200;
                response.WriteResponse(presets.GetAll());
            }
            else
            {
                string result = presets.Get(presetName);
                if (result == null)
                {
                    response.StatusCode = 404;
                    response.WriteResponse("No such preset has been registered");
                }
                else
                {
                    response.StatusCode = 200;
                    response.WriteResponse(result);
                }
            }
        }
Example #2
0
        public void HandleDelete(HttpListenerRequest request, HttpListenerResponse response)
        {
            string queryString = request.Url.Query;
            var    queryParts  = Server.ParseQueryString(queryString);

            string jobID = queryParts.GetFirstValue("jobid");


            if (string.IsNullOrEmpty(jobID))
            {
                response.WriteResponse(404, "Not Found");
            }
            else
            {
                try
                {
                    log.Info("Deleting job: " + jobID);
                    TranscodeJob job;
                    if (!jobs.TryGetValue(jobID, out job))
                    {
                        job.CleanUp();
                        jobs.TryRemove(job.JobToken, out job);
                    }


                    if (FFRest.config["mode"] == "move")
                    {
                        if (Directory.Exists(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["thumb-destination"] + Path.DirectorySeparatorChar + jobID))
                        {
                            DirectoryInfo di = new DirectoryInfo(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["thumb-destination"] + Path.DirectorySeparatorChar + jobID);
                            foreach (var file in di.GetFiles())
                            {
                                file.Delete();
                            }
                            di.Delete();
                        }
                        if (Directory.Exists(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + jobID))
                        {
                            DirectoryInfo di = new DirectoryInfo(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + jobID);
                            foreach (var file in di.GetFiles())
                            {
                                file.Delete();
                            }
                            di.Delete();
                        }
                    }
                    response.WriteResponse(200, "Deleted");
                }
                catch (Exception ex)
                {
                    log.Debug("Failed to process request", ex);
                    response.WriteResponse(500, "Server Error");
                }
            }
        }
Example #3
0
        public void HandleGet(System.Net.HttpListenerRequest request, System.Net.HttpListenerResponse response)
        {
            string queryString = request.Url.Query;
            var    queryParts  = Server.ParseQueryString(queryString);

            string type = queryParts.GetFirstValue("type");

            if (string.IsNullOrEmpty(type))
            {
                response.WriteResponse(400, "Missing parameter type");
                return;
            }
            string data = "";
            int    exitCode;

            switch (type.ToLower())
            {
            case "formats":
                data = FFmpeg.Exec("-formats", out exitCode);
                break;

            case "codecs":
                data = FFmpeg.Exec("-codecs", out exitCode);
                break;

            case "bsfs":
                data = FFmpeg.Exec("-bsfs", out exitCode);
                break;

            case "protocols":
                data = FFmpeg.Exec("-protocols", out exitCode);
                break;

            case "pix_fmts":
                data = FFmpeg.Exec("-pix_fmts", out exitCode);
                break;

            case "help":
                data = FFmpeg.Exec("-help", out exitCode);
                break;
            }

            response.WriteResponse(200, data);
        }
Example #4
0
        public void HandleGet(System.Net.HttpListenerRequest request, System.Net.HttpListenerResponse response)
        {
            string queryString = request.Url.Query;
            var    queryParts  = Server.ParseQueryString(queryString);

            string jobID = queryParts.GetFirstValue("jobid");

            if (string.IsNullOrEmpty(jobID))
            {
                response.WriteResponse(400, "Missing parameter jobID");
                return;
            }

            TranscodeJob job;

            if (!jobs.TryGetValue(jobID, out job))
            {
                response.WriteResponse(404, "Job Not Found");
                return;
            }


            // Build m3u8 file data
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("#EXTM3U");
            foreach (var task in job.Results.OrderBy(x => x.BitRate))
            {
                if (task.HasSegments)
                {
                    sb.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=" + (task.BitRate));
                    sb.AppendLine(task.Playlist);
                }
            }
            response.WriteResponse(200, sb.ToString());
        }
Example #5
0
        public void HandleGet(HttpListenerRequest request, HttpListenerResponse response)
        {
            string queryString = request.Url.Query;
            var    queryParts  = Server.ParseQueryString(queryString);

            string jobID = queryParts.GetFirstValue("jobid");


            if (string.IsNullOrEmpty(jobID))
            {
                var jobs = this.jobs.Values.ToList();
                response.WriteResponse(200, jobs);
            }
            else
            {
                TranscodeJob job;
                if (!jobs.TryGetValue(jobID, out job))
                {
                    response.WriteResponse(404, "Job Not Found");
                    return;
                }
                response.WriteResponse(200, job);
            }
        }
        public object Handle()
        {
            try
            {
                if (handler == null)
                {
                    log.Error("HTTP Request Handler is null");
                }
                switch (context.Request.HttpMethod.ToLower())
                {
                case "get":

                    handler.HandleGet(context.Request, context.Response);
                    break;

                case "head":
                    if (handler is IHttpExtendedRequestHandler)
                    {
                        ((IHttpExtendedRequestHandler)handler).HandleHead(context.Request, context.Response);
                    }
                    else
                    {
                        context.Response.StatusCode = 404;
                        //context.Response.Close();
                    }
                    break;

                case "delete":
                    if (handler is IHttpExtendedRequestHandler)
                    {
                        ((IHttpExtendedRequestHandler)handler).HandleDelete(context.Request, context.Response);
                    }
                    else
                    {
                        context.Response.StatusCode = 404;
                        //context.Response.Close();
                    }
                    break;

                case "post":
                    Server.RequestData data = null;
                    // Handle multipart requests with file data
                    if (context.Request.ContentType != null && context.Request.ContentType.StartsWith("multipart/form-data;"))
                    {
                        var boundary = "--" + context.Request.ContentType.Substring(30);

                        byte[]       buffer  = new byte[4096];
                        int          read    = 0;
                        MemoryStream mstream = new MemoryStream();
                        while ((read = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            mstream.Write(buffer, 0, read);
                        }
                        try
                        {
                            data = processMultipartData(mstream, boundary);
                        }
                        catch (Exception ex)
                        {
                            log.Error("Failed to process http post request", ex);
                            context.Response.WriteResponse(500, ex.Message);
                            return(null);
                        }
                    }
                    else
                    {
                        var post_string = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding).ReadToEnd();
                        var parts       = Server.ParseQueryString(post_string);
                        data = new Server.RequestData();
                        data.PostParameters = parts;
                    }

                    handler.HandlePost(context.Request, context.Response, data);
                    break;
                }
            }
            catch (Exception ex)
            {
                log.Error("HTTP Processing Exception", ex);
                if (context != null)
                {
                    if (context.Response != null)
                    {
                        if (context.Response.OutputStream.CanWrite)
                        {
                            try
                            {
                                context.Response.WriteResponse(500, ex.ToString());
                            }
                            catch (Exception innerException)
                            {
                                log.Error("Failed to write response", innerException);
                                //Console.WriteLine(innerException);
                            }
                        }
                    }
                }
            }

            try
            {
                if (context.Response.OutputStream.CanWrite)
                {
                    context.Response.Close();
                }
            }
            catch (Exception ex)
            {
                log.Error("Error closing stream", ex);
            }
            return(null);
        }