public static string GetContentMediaUrlLocal(ContentMedia contentMedia) { return String.Format("{0}/Media/Stream/{1}?version={2}", "", //SystemConfig.BaseUrl, contentMedia.ContentId.ToString(), (MediaVersion)contentMedia.MediaVersion); }
public static string GetContentMediaPathLocal(ContentMedia contentMedia) { var assetPath = (MediaVersion)contentMedia.MediaVersion == MediaVersion.Full ? SystemConfig.MediaPathFull : SystemConfig.MediaPathPreview; return Path.Combine(assetPath, GetContentMediaFileName(contentMedia.ContentId)); }
public static byte[] GetContentMedia(ContentMedia contentMedia, User user) { //try { return GetContentMedia(contentMedia); //} //catch (Exception ex) { // Log.Error(ex); // throw ex; //} }
/// <summary> /// Create a new ContentMedia object. /// </summary> /// <param name="contentId">Initial value of the ContentId property.</param> /// <param name="mediaVersion">Initial value of the MediaVersion property.</param> /// <param name="mediaDate">Initial value of the MediaDate property.</param> /// <param name="isRemote">Initial value of the IsRemote property.</param> public static ContentMedia CreateContentMedia(global::System.Int32 contentId, global::System.Int32 mediaVersion, global::System.DateTime mediaDate, global::System.Boolean isRemote) { ContentMedia contentMedia = new ContentMedia(); contentMedia.ContentId = contentId; contentMedia.MediaVersion = mediaVersion; contentMedia.MediaDate = mediaDate; contentMedia.IsRemote = isRemote; return contentMedia; }
// ************************************** // GetContentMedia // ************************************** public static void GetContentMedia(string target, ContentMedia contentMedia) { using (var awsclient = Amazon.AWSClientFactory.CreateAmazonS3Client( RemoteMediaConfiguration.AccessKeyID, RemoteMediaConfiguration.SecretAccessKeyID)) { var key = GetContentMediaKey(contentMedia); var request = new GetObjectRequest() .WithBucketName(RemoteMediaConfiguration.BucketName) .WithKey(key); try { using (S3Response response = awsclient.GetObject(request)) { using (Stream s = response.ResponseStream) { using (var fs = File.OpenWrite(target)) { byte[] data = new byte[262144];//[32768]; int bytesRead = 0; do { bytesRead = s.Read(data, 0, data.Length); fs.Write(data, 0, bytesRead); } while (bytesRead > 0); fs.Flush(); fs.Close(); } } } } catch (AmazonS3Exception amazonS3Exception) { Log.Error(amazonS3Exception); } } }
//: IMediaCloudService { // ************************************** // GetContentKey // ************************************** public static string GetContentKey(ContentMedia contentMedia) { return String.Concat(GetContentPrefix((MediaVersion)contentMedia.MediaVersion), contentMedia.ContentId, SystemConfig.MediaDefaultExtension); }
// ************************************** // GetContentMediaKey // ************************************** private static string GetContentMediaKey(ContentMedia contentMedia) { var key = String.Format(RemoteMediaConfiguration.MediaUrlFormat, (MediaVersion)contentMedia.MediaVersion, contentMedia.ContentId); return key; }
// ************************************** // PutContentMedia // ************************************** public static void PutContentMedia(string source, ContentMedia contentMedia) { using (var awsclient = Amazon.AWSClientFactory.CreateAmazonS3Client(RemoteMediaConfiguration.AccessKeyID, RemoteMediaConfiguration.SecretAccessKeyID)) { var key = GetContentMediaKey(contentMedia); // simple object put PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(RemoteMediaConfiguration.BucketName) .WithFilePath(source) .WithKey(key) .WithStorageClass(S3StorageClass.ReducedRedundancy); awsclient.PutObject(request); } }
// ************************************** // GetContentMediaUrl // ************************************** public static string GetContentMediaUrl(ContentMedia contentMedia) { using (var awsclient = Amazon.AWSClientFactory.CreateAmazonS3Client( RemoteMediaConfiguration.AccessKeyID, RemoteMediaConfiguration.SecretAccessKeyID)) { var key = GetContentMediaKey(contentMedia); var request = new GetPreSignedUrlRequest() .WithProtocol(Amazon.S3.Model.Protocol.HTTP) .WithBucketName(RemoteMediaConfiguration.BucketName) .WithKey(key) .WithExpires(DateTime.Now.Date.AddDays(1)); return awsclient.GetPreSignedURL(request); } }
//: BaseService, IMediaService { // ************************************** // GetContentMedia // ************************************** public static byte[] GetContentMedia(ContentMedia contentMedia) { return SystemConfig.UseRemoteMedia && contentMedia.IsRemote ? AmazonCloudService.GetContentMedia(contentMedia) : GetContentMediaLocal(contentMedia); }
private static ContentMedia UpdateMediaId3info(ContentMedia media, FileInfo file) { var id3tag = ID3Reader.GetID3Metadata(file.FullName); media.MediaSize = file.Length; media.MediaLength = id3tag.MediaLength; media.MediaBitRate = id3tag.GetBitRate(file.Length); return media; }
// ************************************** // RepushMedia // ************************************** private static void RepushMedia(Content content, ContentMedia media, string filePath, FileInfo file) { AmazonCloudService.GetContentMedia(filePath, media); var tempFile = new FileInfo(filePath); Log.Debug(String.Format("Remote file for {0} downloaded", content.ContentId)); UpdateContentId3Tag(content, tempFile); UpdateMediaId3info(media, tempFile); AmazonCloudService.PutContentMedia(tempFile.FullName, media); Log.Debug(String.Format("Re-uploaded remote file {0}", file.FullName)); //File.Delete(tempFile.FullName); }
// ************************************** // SaveContentMedia // ************************************** public static void SaveContentMedia(string filePath, ContentMedia contentMedia) { //ID3Writer.NormalizeTag(filePath, content); var mediaPath = GetContentMediaPathLocal(contentMedia); FileSystem.SafeMove(filePath, mediaPath, true); }
public static string GetContentMediaUrl(ContentMedia contentMedia) { return SystemConfig.UseRemoteMedia && contentMedia.IsRemote ? AmazonCloudService.GetContentMediaUrl(contentMedia) : GetContentMediaUrlLocal(contentMedia); }
/// <summary> /// Deprecated Method for adding a new object to the ContentMedia EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToContentMedia(ContentMedia contentMedia) { base.AddObject("ContentMedia", contentMedia); }
// ************************************** // GetContentMedia // ************************************** public static byte[] GetContentMedia(ContentMedia contentMedia) { byte[] mediaBytes = null; using (var awsclient = Amazon.AWSClientFactory.CreateAmazonS3Client( RemoteMediaConfiguration.AccessKeyID, RemoteMediaConfiguration.SecretAccessKeyID)) { var key = GetContentMediaKey(contentMedia); var request = new GetObjectRequest() .WithBucketName(RemoteMediaConfiguration.BucketName) .WithKey(key); try { using (S3Response response = awsclient.GetObject(request)) { using (Stream s = response.ResponseStream) { using (var fs = new MemoryStream()) { byte[] data = new byte[contentMedia.MediaSize.GetValueOrDefault(262144)]; int bytesRead = 0; do { bytesRead = s.Read(data, 0, data.Length); fs.Write(data, 0, bytesRead); } while (bytesRead > 0); mediaBytes = fs.ToArray(); return mediaBytes; } } } } catch (AmazonS3Exception amazonS3Exception) { Log.Error(amazonS3Exception); return mediaBytes; } } }
// ************************************** // SaveCatalog // ************************************** private CatalogUploadState SaveCatalog(CatalogUploadState state) { System.Diagnostics.Debug.Write("Step5"); var user = Account.User(); // Save/create Catalog if (user.IsAtLeastInRole(Roles.Admin) && !user.MyBalances().NumberOfSongs.IsAtTheLimit) { using (var ctx = new SongSearchContext()) { var catalog = ctx.Catalogs.SingleOrDefault(c => c.CatalogName.ToUpper() == state.CatalogName) ?? new Catalog() { CatalogName = state.CatalogName, CreatedByUserId = user.UserId, CreatedOn = DateTime.Now }; if (catalog.CatalogId == 0) { ctx.Catalogs.AddObject(catalog); //DataSession.CommitChanges(); //Make current user an admin var userCatalog = new UserCatalogRole() { UserId = user.UserId, CatalogId = catalog.CatalogId, RoleId = (int)Roles.Admin }; ctx.UserCatalogRoles.AddObject(userCatalog); //Make parent user an admin if (user.ParentUserId.HasValue) { var parentUserCatalog = //DataSession.Single<UserCatalogRole>( // x => x.UserId == user.ParentUserId.Value && // x.CatalogId == catalog.CatalogId && // x.RoleId == (int)Roles.Admin // ) ?? new UserCatalogRole() { UserId = user.ParentUserId.Value, CatalogId = catalog.CatalogId, RoleId = (int)Roles.Admin }; ctx.UserCatalogRoles.AddObject(parentUserCatalog); } //Make plan user an admin if (!user.IsPlanOwner && user.PlanUserId != user.ParentUserId.GetValueOrDefault()) { var planUserCatalog = //DataSession.Single<UserCatalogRole>( // x => x.UserId == user.PlanUserId && // x.CatalogId == catalog.CatalogId && // x.RoleId == (int)Roles.Admin // ) ?? new UserCatalogRole() { UserId = user.PlanUserId, CatalogId = catalog.CatalogId, RoleId = (int)Roles.Admin }; ctx.UserCatalogRoles.AddObject(planUserCatalog); } // defer? ctx.SaveChanges(); } state.CatalogId = catalog.CatalogId; state.CatalogName = catalog.CatalogName.ToUpper(); // Save Content var content = App.IsLicensedVersion ? state.Content.Take(user.MyBalances().NumberOfSongs.IsGoodFor(state.Content.Count())).ToList() : state.Content; foreach (var itm in content) { itm.CatalogId = state.CatalogId; itm.CreatedByUserId = user.UserId; itm.CreatedOn = DateTime.Now; itm.LastUpdatedByUserId = user.UserId; itm.LastUpdatedOn = DateTime.Now; //itm.IsMediaOnRemoteServer = false; itm.Title = itm.Title.AsEmptyIfNull();//.CamelCase();//.ToUpper(); itm.Artist = itm.Artist.AsEmptyIfNull();//.CamelCase();//.ToUpper(); itm.RecordLabel = itm.RecordLabel.AsEmptyIfNull();//.CamelCase();//.ToUpper(); itm.ReleaseYear = itm.ReleaseYear.GetValueOrDefault().AsNullIfZero(); itm.Notes = itm.Notes; var full = itm.UploadFiles.SingleOrDefault(f => f.FileMediaVersion == MediaVersion.Full); foreach (var version in ModelEnums.MediaVersions()) { var upl = itm.UploadFiles.SingleOrDefault(f => f.FileMediaVersion == version); if (upl != null) { var file = new FileInfo(upl.FilePath); var id3 = ID3Writer.NormalizeTag(upl.FilePath, itm); var media = new ContentMedia() { MediaVersion = (int)version, MediaType = "mp3", MediaSize = file.Length, MediaLength = id3.MediaLength, MediaDate = file.GetMediaDate(), MediaBitRate = id3.GetBitRate(file.Length) }; media.IsRemote = false; itm.ContentMedia.Add(media); } } ctx.Contents.AddObject(itm); ctx.AddToSongsBalance(user); ctx.SaveChanges(); if (itm.ContentId > 0) { foreach (var file in itm.UploadFiles) { MediaService.SaveContentMedia(file.FilePath, itm.Media(file.FileMediaVersion)); } } } } } SessionService.Session().InitializeSession(true); CacheService.InitializeApp(true); return state; }
private static byte[] GetContentMediaLocal(ContentMedia contentMedia) { var assetFile = new FileInfo(GetContentMediaPathLocal(contentMedia)); if (assetFile.Exists) { var assetBytes = File.ReadAllBytes(assetFile.FullName); return assetBytes; } else { var ex = new ArgumentOutOfRangeException("Content media file is missing"); Log.Error(ex); throw ex; } }