/// <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); } }
/// <summary> /// Writes the content of a buffer into a LargeObject. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="largeObject">The large object.</param> /// <param name="rpu">The rpu.</param> /// <param name="caller">The calling object.</param> /// <remarks>Documented by Dev02, 2008-08-08</remarks> private void BufferToLargeObject(byte[] buffer, LargeObject largeObject, StatusMessageReportProgress rpu, object caller) { largeObject.Seek(0); int offset = 0; int size = buffer.Length; StatusMessageEventArgs args = new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, buffer.Length); while (offset < size) { largeObject.Write(buffer, offset, Math.Min(chunkSize, size - offset)); offset += chunkSize; args.Progress = offset; if (rpu != null) { rpu(args, caller); } } }
/// <summary> /// Writes the content of a buffer into a LargeObject. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="largeObject">The large object.</param> /// <param name="rpu">The rpu.</param> /// <param name="caller">The calling object.</param> /// <remarks>Documented by Dev02, 2008-08-08</remarks> private void BufferToLargeObject(byte[] buffer, LargeObject largeObject, StatusMessageReportProgress rpu, object caller) { largeObject.Seek(0); int offset = 0; int size = buffer.Length; StatusMessageEventArgs args = new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, buffer.Length); while (offset < size) { largeObject.Write(buffer, offset, Math.Min(chunkSize, size - offset)); offset += chunkSize; args.Progress = offset; if (rpu != null) rpu(args, caller); } }
/// <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; } }
/// <summary> /// Creates a new media object. /// </summary> /// <param name="caller">The calling object.</param> /// <param name="rpu">A delegate of the type StatusMessageReportProgress.</param> /// <param name="type">The type.</param> /// <param name="path">The path.</param> /// <param name="isActive">if set to <c>true</c> [is active].</param> /// <param name="isDefault">if set to <c>true</c> [is default].</param> /// <param name="isExample">if set to <c>true</c> [is example].</param> /// <returns></returns> /// <remarks>Documented by Dev02, 2008-08-11</remarks> internal IMedia CreateNewMediaObject(object caller, StatusMessageReportProgress rpu, EMedia type, string path, bool isActive, bool isDefault, bool isExample) { IMedia media = null; Uri uri; if (!this.HasPermission(PermissionTypes.CanModifyMedia)) { throw new PermissionException(); } if (path == null) { throw new ArgumentNullException("Null value not allowed for media file path!"); } try { if (File.Exists(Path.Combine(Environment.CurrentDirectory, path))) //to allow relative paths { path = Path.Combine(Environment.CurrentDirectory, path); } uri = new Uri(path); } catch (UriFormatException exception) { throw new FileNotFoundException("Uri format is invalid.", exception); } if (uri.Scheme == Uri.UriSchemeFile && uri.IsFile) //we got a new file { if (File.Exists(path)) { int newid; using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) newid = mediaconnector.CreateMedia(stream, type, rpu, caller); media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent); Helper.UpdateMediaProperties(path, newid, mediaconnector); } else { throw new FileNotFoundException("Media file could not be found.", path); } } else if (uri.Scheme == "http" && uri.IsLoopback) //we got a http reference => file is already in db { if (DbMediaServer.DbMediaServer.Instance(parent).IsYours(uri)) { int mediaId = DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath); media = DbMedia.CreateDisconnectedCardMedia(mediaId, type, isDefault, isExample, parent); rpu(new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, 100, 100), caller); } else { DbMediaServer.DbMediaServer server = DbMediaServer.DbMediaServer.Instance(uri); int newid = mediaconnector.CreateMedia(GetMediaConnector(server.Parent).GetMediaStream(DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath)), type, rpu, caller); media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent); Helper.UpdateMediaProperties(path, newid, mediaconnector); } } return(media); }
/// <summary> /// Creates a new media object. /// </summary> /// <param name="type">The type.</param> /// <param name="path">The path.</param> /// <param name="isActive">if set to <c>true</c> [is active].</param> /// <param name="isDefault">if set to <c>true</c> [is default].</param> /// <param name="isExample">if set to <c>true</c> [is example].</param> /// <returns></returns> /// <remarks>Documented by Dev02, 2008-08-11</remarks> /// <remarks>Documented by Dev03, 2009-01-13</remarks> public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample) { StatusMessageReportProgress rpu = new StatusMessageReportProgress(ReportProgressUpdate); return(CreateNewMediaObject(this, rpu, type, path, isActive, isDefault, isExample)); }
/// <summary> /// Creates the media. /// </summary> /// <param name="type">The type of the media file.</param> /// <param name="path">The path to the media file.</param> /// <param name="isActive">if set to <c>true</c> [is active].</param> /// <param name="isDefault">if set to <c>true</c> [is default].</param> /// <param name="isExample">if set to <c>true</c> [is example].</param> /// <returns></returns> /// <remarks>Documented by Dev03, 2007-09-03</remarks> /// <remarks>Documented by Dev03, 2009-01-13</remarks> public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample) { StatusMessageReportProgress rpu = new StatusMessageReportProgress(ReportProgressUpdate); return (parent.GetParentDictionary() as DbDictionary).CreateNewMediaObject(this, rpu, type, path, isActive, isDefault, isExample); }
/// <summary> /// Creates a new media object. /// </summary> /// <param name="caller">The calling object.</param> /// <param name="rpu">A delegate of the type StatusMessageReportProgress.</param> /// <param name="type">The type.</param> /// <param name="path">The path.</param> /// <param name="isActive">if set to <c>true</c> [is active].</param> /// <param name="isDefault">if set to <c>true</c> [is default].</param> /// <param name="isExample">if set to <c>true</c> [is example].</param> /// <returns></returns> /// <remarks>Documented by Dev02, 2008-08-11</remarks> internal IMedia CreateNewMediaObject(object caller, StatusMessageReportProgress rpu, EMedia type, string path, bool isActive, bool isDefault, bool isExample) { IMedia media = null; Uri uri; if (!this.HasPermission(PermissionTypes.CanModifyMedia)) throw new PermissionException(); if (path == null) throw new ArgumentNullException("Null value not allowed for media file path!"); try { if (File.Exists(Path.Combine(Environment.CurrentDirectory, path))) //to allow relative paths path = Path.Combine(Environment.CurrentDirectory, path); uri = new Uri(path); } catch (UriFormatException exception) { throw new FileNotFoundException("Uri format is invalid.", exception); } if (uri.Scheme == Uri.UriSchemeFile && uri.IsFile) //we got a new file { if (File.Exists(path)) { int newid; using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) newid = mediaconnector.CreateMedia(stream, type, rpu, caller); media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent); Helper.UpdateMediaProperties(path, newid, mediaconnector); } else throw new FileNotFoundException("Media file could not be found.", path); } else if (uri.Scheme == "http" && uri.IsLoopback) //we got a http reference => file is already in db { if (DbMediaServer.DbMediaServer.Instance(parent).IsYours(uri)) { int mediaId = DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath); media = DbMedia.CreateDisconnectedCardMedia(mediaId, type, isDefault, isExample, parent); rpu(new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, 100, 100), caller); } else { DbMediaServer.DbMediaServer server = DbMediaServer.DbMediaServer.Instance(uri); int newid = mediaconnector.CreateMedia(GetMediaConnector(server.Parent).GetMediaStream(DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath)), type, rpu, caller); media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent); Helper.UpdateMediaProperties(path, newid, mediaconnector); } } return media; }