Esempio n. 1
0
        /// <summary>
        /// Updates the media.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="media">The media.</param>
        /// <remarks>Documented by Dev02, 2008-08-06</remarks>
        /// <remarks>Documented by Dev03, 2009-01-13</remarks>
        public void UpdateMedia(int id, Stream media)
        {
            using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                NpgsqlTransaction tran = con.BeginTransaction();

                int noid;
                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id";
                    cmd.Parameters.Add("id", id);
                    noid = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser));
                }

                LargeObjectManager lbm = new LargeObjectManager(con);
                lbm.Delete(noid);

                noid = lbm.Create(LargeObjectManager.READWRITE);
                LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                byte[]      buffer      = new byte[media.Length];
                media.Read(buffer, 0, (int)media.Length);
                BufferToLargeObject(buffer, largeObject);
                largeObject.Close();

                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "UPDATE \"MediaContent\" SET data=:data WHERE id=:id";
                    cmd.Parameters.Add("id", id);
                    cmd.Parameters.Add("data", noid);
                    PostgreSQLConn.ExecuteNonQuery(cmd, Parent.CurrentUser);
                }

                tran.Commit();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new media object.
        /// </summary>
        /// <param name="media">The memory stream containing the media.</param>
        /// <param name="type">The media type.</param>
        /// <param name="rpu">A delegate of type <see cref="StatusMessageReportProgress"/> used to send messages back to the calling object.</param>
        /// <param name="caller">The calling object.</param>
        /// <returns>The id for the new media object.</returns>
        /// <remarks>Documented by Dev03, 2008-08-05</remarks>
        /// <remarks>Documented by Dev03, 2009-01-13</remarks>
        public int CreateMedia(Stream media, EMedia type, StatusMessageReportProgress rpu, object caller)
        {
            using (NpgsqlConnection conn = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                NpgsqlTransaction tran = conn.BeginTransaction();

                LargeObjectManager lbm  = new LargeObjectManager(conn);
                int         noid        = lbm.Create(LargeObjectManager.READWRITE);
                LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                byte[]      buffer      = new byte[media.Length];
                media.Read(buffer, 0, (int)media.Length);
                BufferToLargeObject(buffer, largeObject, rpu, caller);
                largeObject.Close();

                int newId = 0;
                using (NpgsqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO \"MediaContent\" (data, media_type) VALUES (:data, :type) RETURNING id;";
                    cmd.Parameters.Add("data", noid);
                    cmd.Parameters.Add("type", type.ToString());
                    newId = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser));
                }
                tran.Commit();

                return(newId);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// modify a record
        /// </summary>
        public void modifyz_voluntarios(z_voluntarios myz_voluntarios, string foto)
        {
            CnxBase myBase = new CnxBase();
            string  reqSQL;

            reqSQL = "UPDATE z_voluntarios SET id_voluntario=" + myz_voluntarios.id_voluntario + ",id_compania=" + myz_voluntarios.id_compania + ",nombres='" + myz_voluntarios.nombres + "',apellidos='" + myz_voluntarios.apellidos + "',rut='" + myz_voluntarios.rut + "',direccion='" + myz_voluntarios.direccion + "',fecha_nacimiento='" + myz_voluntarios.fecha_nacimiento + "',ingreso='" + myz_voluntarios.ingreso + "',num_llamado=" + myz_voluntarios.num_llamado + ",comuna='" + myz_voluntarios.comuna + "',telefono='" + myz_voluntarios.telefono + "',celular='" + myz_voluntarios.celular + "' WHERE (id_voluntario=" + myz_voluntarios.id_voluntario + ")";


            try
            {
                NpgsqlConnection myConn = myBase.OpenConnection(myBase.cnxString);

                if (foto != null)
                {
                    NpgsqlTransaction  t   = myConn.BeginTransaction();
                    LargeObjectManager lbm = new LargeObjectManager(myConn);

                    int         noid = lbm.Create(LargeObjectManager.READWRITE);
                    LargeObject lo   = lbm.Open(noid, LargeObjectManager.READWRITE);

                    // eliminar antiguo
                    NpgsqlCommand comm = new NpgsqlCommand("select foto from z_voluntarios where id_voluntario=" + myz_voluntarios.id_voluntario, myConn);
                    comm.ExecuteScalar();

                    //lbm.Unlink(oid);

                    FileStream fs = File.OpenRead(foto);

                    byte[] buf = new byte[fs.Length];
                    fs.Read(buf, 0, (int)fs.Length);

                    lo.Write(buf);
                    lo.Close();
                    t.Commit();

                    reqSQL = "UPDATE z_voluntarios SET id_voluntario=" + myz_voluntarios.id_voluntario + ",id_compania=" + myz_voluntarios.id_compania + ",nombres='" + myz_voluntarios.nombres + "',apellidos='" + myz_voluntarios.apellidos + "',rut='" + myz_voluntarios.rut + "',direccion='" + myz_voluntarios.direccion + "',fecha_nacimiento='" + myz_voluntarios.fecha_nacimiento + "',ingreso='" + myz_voluntarios.ingreso + "',num_llamado=" + myz_voluntarios.num_llamado + ",comuna='" + myz_voluntarios.comuna + "',telefono='" + myz_voluntarios.telefono + "',celular='" + myz_voluntarios.celular + "', foto=" + noid + " WHERE (id_voluntario=" + myz_voluntarios.id_voluntario + ")";
                }

                NpgsqlCommand myCommand = new NpgsqlCommand(reqSQL, myConn);
                myCommand.ExecuteNonQuery();
                myBase.CloseConnection(myConn);
            }
            catch (Exception myErr)
            {
                throw (new Exception(myErr.ToString() + reqSQL));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// add a record
        /// </summary>
        /// <param name="myID"></param>
        public void addz_voluntarios(z_voluntarios myz_voluntarios, string foto)
        {
            CnxBase myBase = new CnxBase();
            string  reqSQL;

            reqSQL = "INSERT INTO z_voluntarios (id_compania,nombres,apellidos,rut,direccion,fecha_nacimiento,ingreso,num_llamado,comuna,telefono,celular) VALUES (" + myz_voluntarios.id_compania + ",'" + myz_voluntarios.nombres + "','" + myz_voluntarios.apellidos + "','" + myz_voluntarios.rut + "','" + myz_voluntarios.direccion + "','" + myz_voluntarios.fecha_nacimiento + "','" + myz_voluntarios.ingreso + "'," + myz_voluntarios.num_llamado + ",'" + myz_voluntarios.comuna + "','" + myz_voluntarios.telefono + "','" + myz_voluntarios.celular + "')";
            try
            {
                NpgsqlConnection myConn = myBase.OpenConnection(myBase.cnxString);

                if (foto != null)
                {
                    NpgsqlTransaction  t   = myConn.BeginTransaction();
                    LargeObjectManager lbm = new LargeObjectManager(myConn);

                    int         noid = lbm.Create(LargeObjectManager.READWRITE);
                    LargeObject lo   = lbm.Open(noid, LargeObjectManager.READWRITE);

                    FileStream fs = File.OpenRead(foto);

                    byte[] buf = new byte[fs.Length];
                    fs.Read(buf, 0, (int)fs.Length);

                    lo.Write(buf);
                    lo.Close();
                    t.Commit();

                    reqSQL = "INSERT INTO z_voluntarios (id_compania,nombres,apellidos,rut,direccion,fecha_nacimiento,ingreso,num_llamado,comuna,telefono,celular, foto) VALUES (" + myz_voluntarios.id_compania + ",'" + myz_voluntarios.nombres + "','" + myz_voluntarios.apellidos + "','" + myz_voluntarios.rut + "','" + myz_voluntarios.direccion + "','" + myz_voluntarios.fecha_nacimiento + "','" + myz_voluntarios.ingreso + "'," + myz_voluntarios.num_llamado + ",'" + myz_voluntarios.comuna + "','" + myz_voluntarios.telefono + "','" + myz_voluntarios.celular + "', " + noid + ")";
                }

                NpgsqlCommand myCommand = new NpgsqlCommand(reqSQL, myConn);
                myCommand.ExecuteNonQuery();
                myBase.CloseConnection(myConn);
            }
            catch (Exception myErr)
            {
                throw (new Exception(myErr.ToString() + reqSQL));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Sets the extension stream.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <param name="extensionStream">The extension stream.</param>
        public void SetExtensionStream(Guid guid, Stream extensionStream)
        {
            using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                NpgsqlTransaction tran = con.BeginTransaction();

                LargeObjectManager lbm  = new LargeObjectManager(con);
                int         noid        = lbm.Create(LargeObjectManager.READWRITE);
                LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                byte[]      buffer      = new byte[extensionStream.Length];
                extensionStream.Read(buffer, 0, (int)extensionStream.Length);
                BufferToLargeObject(buffer, largeObject);
                largeObject.Close();

                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "UPDATE \"Extensions\" SET data=:data WHERE guid=:guid";
                    cmd.Parameters.Add("data", noid);
                    cmd.Parameters.Add("guid", guid.ToString());
                    PostgreSQLConn.ExecuteNonQuery(cmd, Parent.CurrentUser);
                }
                tran.Commit();
            }
        }
Esempio n. 6
0
        public DBFile Upload(string md5, string path_to_contents, string filename, string extension, bool hidden, string compressed_mime)
        {
            IDbTransaction     transaction = null;
            LargeObjectManager manager;
            LargeObject        obj;
            int?   oid;
            DBFile result;
            long   filesize;
            string gzFilename = null;

            try {
                filesize = new FileInfo(path_to_contents).Length;
                if (filesize > 1024 * 1024 * 500)
                {
                    throw new Exception("Max file size is 500 MB");
                }

                using (IDbCommand cmd = CreateCommand()) {
                    cmd.CommandText = "SELECT * FROM File WHERE md5 = '" + md5 + "'";
                    using (IDataReader reader = cmd.ExecuteReader()) {
                        if (reader.Read())
                        {
                            return(new DBFile(reader));
                        }
                    }
                }

                //Console.WriteLine ("Uploading {0} {1} with compressed mime: {2}", Filename, md5, compressed_mime);

                // The file is not in the database
                // Note: there is a race condition here,
                // the same file might get added to the db before we do it here.
                // not quite sure how to deal with that except retrying the above if the insert below fails.

                if (compressed_mime == MimeTypes.GZ)
                {
                    gzFilename = path_to_contents;
                }
                else
                {
                    gzFilename      = FileUtilities.GZCompress(path_to_contents);
                    compressed_mime = MimeTypes.GZ;
                }

                transaction = BeginTransaction();

                if (Configuration.StoreFilesInDB)
                {
                    manager = new LargeObjectManager(this.dbcon);
                    oid     = manager.Create(LargeObjectManager.READWRITE);
                    obj     = manager.Open(oid.Value, LargeObjectManager.READWRITE);

                    using (FileStream st = new FileStream(gzFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                        byte [] buffer = new byte [1024];
                        int     read   = -1;
                        while (read != 0)
                        {
                            read = st.Read(buffer, 0, buffer.Length);
                            obj.Write(buffer, 0, read);
                        }
                    }
                    obj.Close();
                }
                else
                {
                    oid = null;
                    string fn = FileUtilities.CreateFilename(md5, true, true);

                    File.Copy(gzFilename, fn, true);
                    log.DebugFormat("Saved file to: {0}", fn);
                }

                result          = new DBFile();
                result.file_id  = oid;
                result.filename = Path.GetFileName(filename);
                result.md5      = md5;
                result.size     = (int)filesize;
                result.hidden   = hidden;
                switch (extension.ToLower())
                {
                case ".log":
                case ".stdout":
                case ".stderr":
                    result.mime = MimeTypes.LOG;
                    break;

                case ".txt":
                    result.mime = MimeTypes.TXT;
                    break;

                case ".htm":
                case ".html":
                    result.mime = MimeTypes.HTML;
                    break;

                case ".png":
                    result.mime = MimeTypes.PNG;
                    break;

                case ".jpg":
                    result.mime = MimeTypes.JPG;
                    break;

                case ".bmp":
                    result.mime = MimeTypes.BMP;
                    break;

                case ".tar":
                    result.mime = MimeTypes.TAR;
                    break;

                case ".bz":
                    result.mime = MimeTypes.BZ;
                    break;

                case ".bz2":
                    result.mime = MimeTypes.BZ2;
                    break;

                case ".zip":
                    result.mime = MimeTypes.ZIP;;
                    break;

                case ".gz":
                    result.mime = MimeTypes.GZ;
                    break;

                case ".xpi":
                    result.mime = MimeTypes.XPI;
                    break;

                case ".crx":
                    result.mime = MimeTypes.CRX;
                    break;

                default:
                    result.mime = MimeTypes.OCTET_STREAM;
                    break;
                }
                result.compressed_mime = compressed_mime;
                result.Save(this);

                transaction.Commit();
                transaction = null;

                return(result);
            } finally {
                FileUtilities.TryDeleteFile(gzFilename);

                if (transaction != null)
                {
                    transaction.Rollback();
                }
            }
        }