Exemplo n.º 1
0
 /// <summary>
 /// Actualiza un fichero mp3 ya existente
 /// </summary>
 /// <param name="fichero"></param>
 /// <returns></returns>
 public static FicheroMP3 ActualizaFicheroMP3(FicheroMP3 fichero)
 {
     LiteDB.LiteCollection <FicheroMP3> ficheros = db.GetCollection <FicheroMP3>("ficheros");
     ficheros.Update(fichero);
     Logger.Log("Actualizado fichero en la Base de datos");
     return(fichero);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Añade un fichero mp3 a la BD
 /// </summary>
 /// <param name="fichero"></param>
 /// <returns></returns>
 public static FicheroMP3 AddFicheroMP3(FicheroMP3 fichero)
 {
     LiteDB.LiteCollection <FicheroMP3> ficheros = db.GetCollection <FicheroMP3>("ficheros");
     ficheros.Insert(fichero);
     ficheros.EnsureIndex(f => f.Usuario);
     Logger.Log("Añadido fichero a la Base de datos");
     return(fichero);
 }
Exemplo n.º 3
0
        public void TestAnadirArchivoMP3()
        {
            BD.Init();
            FicheroMP3 fichero = BD.AddFicheroMP3(new FicheroMP3()
            {
                Estado         = FicheroMP3.EstadosFicheroMP3.Pendiente,
                FechaRecepcion = DateTime.Now,
                Usuario        = "Test"
            });

            Assert.IsTrue(fichero.Id > 0);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Devuelve la transcripción de un fichero mp3 a partir de su usuario y su id. El usuario se usa por seguridad.
        /// </summary>
        /// <param name="usuario"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static RespuestaTranscripcion GetTranscripcion(String usuario, Int32 id)
        {
            Logger.Log("Solicitada transcripción de la Base de datos");
            LiteDB.LiteCollection <FicheroMP3> ficheros = db.GetCollection <FicheroMP3>("ficheros");
            FicheroMP3 fichero = ficheros.Find(f => f.Usuario == usuario && f.Id == id).FirstOrDefault();

            if (fichero == null)
            {
                return(new RespuestaTranscripcion()
                {
                    Codigo = -1,
                    Transcripcion = null
                });
            }
            else if (fichero.Estado == FicheroMP3.EstadosFicheroMP3.Realizada)
            {
                return(new RespuestaTranscripcion()
                {
                    Codigo = 0,
                    Transcripcion = fichero.Transcripcion
                });
            }
            else if (fichero.Estado == FicheroMP3.EstadosFicheroMP3.Error)
            {
                return(new RespuestaTranscripcion()
                {
                    Codigo = -2,
                    Transcripcion = null
                });
            }
            else
            {
                return(new RespuestaTranscripcion()
                {
                    Codigo = -3,
                    Transcripcion = null
                });
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Tarea encargada de ejecutar el proceso HTTP
        /// </summary>
        /// <param name="context"></param>
        private void HttpListenerCallback(HttpListenerContext context)
        {
            byte[] respuesta = new byte[0];
            String data_text = new StreamReader(context.Request.InputStream,
                                                context.Request.ContentEncoding).ReadToEnd();

            try
            {
                String[] ruta = context.Request.RawUrl.Split('/');

                //La ruta es /fichero/{usuario} y es un POST
                if (ruta[1].CompareTo("fichero") == 0 && context.Request.HttpMethod.CompareTo("POST") == 0 && ruta.Length == 3)
                {
                    Logger.Log("Servicio POST fichero iniciado");
                    byte[] fichero = Convert.FromBase64String(data_text);

                    if (fichero.Length > 5 * 1024 * 1024)
                    {
                        context.Response.StatusCode        = 413;
                        context.Response.StatusDescription = "Request Entity Too Large";
                        RespuestaError re = new RespuestaError()
                        {
                            Error = "El fichero no puede ser más grande de 5 MB"
                        };
                        respuesta = Funciones.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(re));
                    }

                    else
                    {
                        FicheroMP3 ficheroMP3 = new FicheroMP3()
                        {
                            Usuario            = ruta[2],
                            FechaRecepcion     = DateTime.UtcNow,
                            Estado             = FicheroMP3.EstadosFicheroMP3.Pendiente,
                            FechaTranscripcion = null,
                            Transcripcion      = null
                        };

                        BD.AddFicheroMP3(ficheroMP3);

                        using (FileStream fs = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + ficheroMP3.Id + ".mp3", FileMode.Create, FileAccess.Write))
                        {
                            fs.Write(fichero, 0, fichero.Length);
                        }

                        context.Response.StatusCode        = 200;
                        context.Response.StatusDescription = "Ok";
                    }
                }
                //La ruta es /ficheros/{usuario} y es un GET. Los parámetros desde y hasta al ser opcionales no van en formato Friendly, sino en Query Parameters
                else if (ruta[1].CompareTo("ficheros") == 0 && context.Request.HttpMethod.CompareTo("GET") == 0 && ruta.Length == 3)
                {
                    Logger.Log("Servicio GET ficheros iniciado");
                    DateTime?desde    = null;
                    DateTime?hasta    = null;
                    Boolean  paramsOK = true;

                    if (context.Request.QueryString["desde"] != null)
                    {
                        desde = Funciones.CheckParamFecha(context.Request.QueryString["desde"], ref respuesta);
                        if (desde == null)
                        {
                            paramsOK = false;
                        }
                    }
                    if (context.Request.QueryString["hasta"] != null)
                    {
                        hasta = Funciones.CheckParamFecha(context.Request.QueryString["hasta"], ref respuesta);

                        if (hasta == null)
                        {
                            paramsOK = false;
                        }
                    }

                    if (paramsOK)
                    {
                        context.Response.StatusCode        = 200;
                        context.Response.StatusDescription = "Ok";
                        respuesta = Funciones.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(BD.GetLista(ruta[2].Split('?')[0], desde, hasta)));
                    }
                    else
                    {
                        context.Response.StatusCode        = 400;
                        context.Response.StatusDescription = "Bad Request";
                    }
                }
                //La ruta es /fichero/{usuario}/{id} y es un GET
                else if (ruta[1].CompareTo("fichero") == 0 && context.Request.HttpMethod.CompareTo("GET") == 0 && ruta.Length == 4)
                {
                    Logger.Log("Servicio GET fichero iniciado");
                    Int32 id;

                    if (Int32.TryParse(ruta[3], out id))
                    {
                        RespuestaTranscripcion rt = BD.GetTranscripcion(ruta[2], id);
                        if (rt.Codigo == 0)
                        {
                            context.Response.StatusCode        = 200;
                            context.Response.StatusDescription = "Ok";
                            respuesta = Funciones.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(rt));
                        }
                        else if (rt.Codigo == -1)
                        {
                            context.Response.StatusCode        = 404;
                            context.Response.StatusDescription = "Not Found";
                        }
                        else if (rt.Codigo == -2)
                        {
                            context.Response.StatusCode        = 410;
                            context.Response.StatusDescription = "Gone";
                        }
                        else
                        {
                            context.Response.StatusCode        = 204;
                            context.Response.StatusDescription = "No Content";
                        }
                    }
                    else
                    {
                        context.Response.StatusCode        = 400;
                        context.Response.StatusDescription = "Bad Request";
                    }
                }
                //Cualquier otro caso la ruta no existe y se devuelve Not Found.
                else
                {
                    context.Response.StatusCode        = 404;
                    context.Response.StatusDescription = "Not Found";
                }
            }
            catch (System.Exception ex)
            {
                //Si se produce una excepción se devuelve un Internal Server Error (500)
                context.Response.StatusCode        = 500;
                context.Response.StatusDescription = "Internal Server Error";

                respuesta = Funciones.GetBytes(ex.ToString());
            }

            context.Response.ContentLength64 = respuesta.Length;
            context.Response.OutputStream.Write(respuesta, 0, respuesta.Length);
            context.Response.Close();
        }