public static void Save(Asset asset, BinaryFile file) { // Check if asset is delegated. Do not inline. bool isDelegated = asset.IsPropertyChanged(Asset.Columns.ContactEmail); // Validate the asset ErrorList errors = ValidateAsset(asset, file); // No point continuing if we have errors if (errors.HasErrors) { throw new InvalidAssetException(errors, asset); } // Delegate asset if required if (isDelegated) { DelegateAsset(asset); } // Truncate asset description to 400 chars if (asset.Description.Length > 400) { asset.Description = asset.Description.Substring(0, 400); } // Update the asset record asset.LastUpdate = DateTime.Now; Asset.Update(asset); Asset.SaveAssetMetadata(asset); // Save the asset file AssetFileManager.SaveAssetFile(asset, file, false); }
public static void ReplaceAssetFile(Asset asset, BinaryFile file, bool notify, User uploadUser) { m_Logger.DebugFormat("ReplaceAssetFile() - AssetId: {0}", asset.AssetId); // Will save the asset file, increment version, etc AssetFileManager.SaveAssetFile(asset, file, notify); if (asset.AssetPublishStatus == AssetPublishStatus.PendingApproval) { // The asset is still in a workflow, which we need to cancel and re-submit // Get the most recent workflow and perform relevant actions on it if (asset.AssetWorkflowList.Count > 0) { // Get the most recent workflow AssetWorkflow assetWorkflow = asset.AssetWorkflowList[0]; // Cancel it WorkflowManager.CancelWorkflow(assetWorkflow); m_Logger.DebugFormat("Cancelled AssetWorkflow - AssetWorkflowId: {0}", assetWorkflow.AssetWorkflowId); // Resubmit it WorkflowManager.SubmitAssetToWorkflow(asset, uploadUser); m_Logger.DebugFormat("Resubmitted asset to workflow. AssetId: {0}", asset.AssetId); } } AuditLogManager.LogAssetAction(asset, uploadUser, AuditAssetAction.ReplacedAssetFile); AuditLogManager.LogUserAction(uploadUser, AuditUserAction.ReplacedAssetFile, string.Format("Replaced asset file of AssetId: {0}", asset.AssetId)); }
/// <summary> /// Saves the asset into the database, and the file to disk. /// </summary> private Asset SaveUploadedAsset(BinaryFile file, string sourceFolder) { // Basic asset values Asset asset = Asset.New(); asset.Filename = file.FileName; asset.UploadDate = DateTime.Now; asset.UploadedByUserId = UploadedBy.UserId.GetValueOrDefault(); asset.BrandId = UploadedBy.PrimaryBrandId; asset.ContactEmail = UploadedBy.Email; asset.UsageRestrictions = UploadedBy.PrimaryBrand.DefaultUsageRestrictionsCopy; asset.CopyrightOwner = UploadedBy.CompanyName; asset.CreateDate = DateTime.Now; asset.LastUpdate = DateTime.Now; // Get the asset type ID based on file extension if the type was not specified asset.AssetTypeId = (AssetTypeId == Int32.MinValue) ? AssetTypeManager.GetAssetTypeId(file.FileExtension) : AssetTypeId; // Set the asset publish status depending on whether the uploading user is required to use workflow asset.AssetPublishStatus = (UploadedBy.UseWorkflow) ? AssetPublishStatus.NotApproved : AssetPublishStatus.Approved; // Set the asset file path (required for referential integrity in the database) asset.AssetFilePathId = AssetFilePathManager.GetDefault().AssetFilePathId.GetValueOrDefault(); // Default download restrictions asset.WatermarkPreview = false; asset.InternalUsers_DownloadApprovalRequired = false; asset.InternalUsers_HideFromUsers = false; asset.ExternalUsers_DownloadApprovalRequired = false; asset.ExternalUsers_HideFromUsers = false; // Default production year to current year asset.ProductionYear = DateTime.Now.Year; // Publish year cannot be null, so default to current year // and set expiry date to 2 years ahead asset.PublishDate = DateTime.Now; asset.ExpiryDate = DateTime.Now.AddYears(2); // Get the base Category which willl be the default brand category if Specify during cataloguing // or the target category if a target category has been passed. Category baseCategory; int categoryId; if (TargetCategoryId == int.MinValue) { baseCategory = CategoryCache.Instance.GetRootCategory(asset.BrandId); } else { baseCategory = CategoryCache.Instance.GetById(TargetCategoryId); } if (sourceFolder != String.Empty && CreateCategorySubFolders) { categoryId = CategoryManager.CreateCategoryTreeFromPath(baseCategory.CategoryId, asset.BrandId, sourceFolder, asset.UploadedByUser); } else { categoryId = baseCategory.CategoryId.GetValueOrDefault(); } // Set processed flag depending on the DoNotProcessForPreview. If this is true // the IsProcessed flag should also be set to true. asset.IsProcessed = DoNotProcessForPreview; // Assign default categories asset.CategoryList.Clear(); asset.CategoryList.Add(CategoryCache.Instance.GetById(categoryId)); asset.PrimaryCategoryId = asset.CategoryList[0].CategoryId.GetValueOrDefault(); OnProgress("Saving asset..."); if (BeforeSave != null) { BeforeSave(this, new AssetEventArgs(asset)); } Asset.Update(asset); Asset.SaveAssetMetadata(asset); OnProgress("Saved with reference: " + asset.AssetId); if (AfterSave != null) { AfterSave(this, new AssetEventArgs(asset)); } if (BeforeFileSave != null) { BeforeFileSave(this, new AssetEventArgs(asset)); } if (!file.IsEmpty) { // Hash should only be checked for non super-admins bool checkHash = (UploadedBy.UserRole != UserRole.SuperAdministrator); // Save the file OnProgress("Saving asset file to disk: " + file.FileName); AssetFileManager.SaveAssetFile(asset, file, SendEmailOnCompletion, checkHash, DoNotProcessForPreview); OnProgress("Saved file '" + file.FileName + "' to disk"); // Fire post-save events if (AfterFileSave != null) { AfterFileSave(this, new AssetEventArgs(asset)); } } m_Logger.DebugFormat("Saved {0} to database, asset ID: {1}", file.FileName, asset.AssetId); return(asset); }