@@ -29,7 +29,8 @@ public class AssetMigration(
2929 ToolConfiguration configuration ,
3030 EntityIdentityFacade entityIdentityFacade ,
3131 IAssetFacade assetFacade ,
32- MediaLinkServiceFactory mediaLinkServiceFactory
32+ MediaLinkServiceFactory mediaLinkServiceFactory ,
33+ InvokedCommands invokedCommands
3334) : IFieldMigration
3435{
3536 public int Rank => 100_000 ;
@@ -40,25 +41,46 @@ context.SourceDataType is KsFieldDataType.DocAttachments or KsFieldDataType.File
4041 Kx13FormControls . UserControlForText . MediaSelectionControl . Equals ( context . SourceFormControl , StringComparison . InvariantCultureIgnoreCase )
4142 ) &&
4243 context . SourceObjectContext
43- // this migration can handle only migration of documents to content items
44- is DocumentSourceObjectContext
44+ is DocumentSourceObjectContext or CustomTableSourceObjectContext
4545 // this migration also handles empty object context - for example when migrating data class, empty context is supplied
4646 or EmptySourceObjectContext ;
4747
4848 public async Task < FieldMigrationResult > MigrateValue ( object ? sourceValue , FieldMigrationContext context )
4949 {
5050 ( string ? _ , string ? sourceFormControl , string ? fieldName , var sourceObjectContext ) = context ;
51- if ( sourceObjectContext is not DocumentSourceObjectContext ( _ , _ , var cmsSite , var oldFormInfo , _ , var documentId ) )
51+
52+ if ( ! invokedCommands . Commands . Any ( x => x is MigrateMediaLibrariesCommand ) )
53+ {
54+ logger . LogError ( $ "Trying to migrate asset field value {{FieldName}}, but commands { MigrateMediaLibrariesCommand . Moniker } and { MigrateAttachmentsCommand . Moniker } were not invoked", fieldName ) ;
55+ return new ( false , null ) ;
56+ }
57+
58+ ICmsSite ? cmsSite ;
59+ CMS . FormEngine . FormInfo ? oldFormInfo ;
60+ int ? documentId ;
61+ if ( sourceObjectContext is DocumentSourceObjectContext docContext )
62+ {
63+ cmsSite = docContext . Site ;
64+ oldFormInfo = docContext . OldFormInfo ;
65+ documentId = docContext . DocumentId ;
66+ }
67+ else if ( sourceObjectContext is CustomTableSourceObjectContext )
68+ {
69+ cmsSite = null ;
70+ oldFormInfo = null ;
71+ documentId = null ;
72+ }
73+ else
5274 {
5375 throw new ArgumentNullException ( nameof ( sourceObjectContext ) ) ;
5476 }
5577
56- var field = oldFormInfo . GetFormField ( fieldName ) ;
78+ var field = oldFormInfo ? . GetFormField ( fieldName ) ;
5779
5880 List < object > mfis = [ ] ;
5981 bool hasMigratedAsset = false ;
6082 if ( sourceValue is string link &&
61- mediaLinkServiceFactory . Create ( ) . MatchMediaLink ( link , cmsSite . SiteID ) is ( true , var mediaLinkKind , var mediaKind , var path , var mediaGuid , _, _) result )
83+ mediaLinkServiceFactory . Create ( ) . MatchMediaLink ( link , cmsSite ? . SiteID ) is ( true , var mediaLinkKind , var mediaKind , var path , var mediaGuid , _, _) result )
6284 {
6385 if ( mediaLinkKind == MediaLinkKind . Path )
6486 {
@@ -131,7 +153,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
131153 {
132154 if ( mediaKind == MediaKind . Attachment )
133155 {
134- switch ( await attachmentMigrator . MigrateAttachment ( mg , $ "__{ fieldName } ", cmsSite . SiteID ) )
156+ switch ( await attachmentMigrator . MigrateAttachment ( mg , $ "__{ fieldName } ", cmsSite ! . SiteID ) )
135157 {
136158 case MigrateAttachmentResultMediaFile ( true , var x , _ ) :
137159 {
@@ -159,8 +181,11 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
159181
160182 if ( mediaKind == MediaKind . MediaFile )
161183 {
162- var sourceMediaFile = modelFacade . SelectWhere < IMediaFile > ( "FileGUID = @mediaFileGuid AND FileSiteID = @fileSiteID" , new SqlParameter ( "mediaFileGuid" , mg ) , new SqlParameter ( "fileSiteID" , cmsSite . SiteID ) )
163- . FirstOrDefault ( ) ;
184+ var sourceMediaFile =
185+ ( cmsSite is not null
186+ ? modelFacade . SelectWhere < IMediaFile > ( "FileGUID = @mediaFileGuid AND FileSiteID = @fileSiteID" , new SqlParameter ( "mediaFileGuid" , mg ) , new SqlParameter ( "fileSiteID" , cmsSite . SiteID ) )
187+ : modelFacade . SelectWhere < IMediaFile > ( "FileGUID = @mediaFileGuid" , new SqlParameter ( "mediaFileGuid" , mg ) )
188+ ) . FirstOrDefault ( ) ;
164189 if ( sourceMediaFile != null )
165190 {
166191 if ( configuration . MigrateMediaToMediaLibrary )
@@ -193,7 +218,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
193218 {
194219 if ( sourceValue is Guid attachmentGuid )
195220 {
196- switch ( await attachmentMigrator . MigrateAttachment ( attachmentGuid , $ "__{ fieldName } ", cmsSite . SiteID ) )
221+ switch ( await attachmentMigrator . MigrateAttachment ( attachmentGuid , $ "__{ fieldName } ", cmsSite ! . SiteID ) )
197222 {
198223 case MigrateAttachmentResultMediaFile ( true , var mfi , _ ) :
199224 {
@@ -221,7 +246,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
221246 }
222247 else if ( sourceValue is string attachmentGuidStr && Guid . TryParse ( attachmentGuidStr , out attachmentGuid ) )
223248 {
224- switch ( await attachmentMigrator . MigrateAttachment ( attachmentGuid , $ "__{ fieldName } ", cmsSite . SiteID ) )
249+ switch ( await attachmentMigrator . MigrateAttachment ( attachmentGuid , $ "__{ fieldName } ", cmsSite ! . SiteID ) )
225250 {
226251 case MigrateAttachmentResultMediaFile { Success : true , MediaFileInfo : { } x } :
227252 {
@@ -260,7 +285,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
260285 if ( documentId is { } docId )
261286 {
262287 var mfisl = new List < object > ( ) ;
263- await foreach ( var migResult in attachmentMigrator . MigrateGroupedAttachments ( docId , field . Guid , field . Name ) )
288+ await foreach ( var migResult in attachmentMigrator . MigrateGroupedAttachments ( docId , field ! . Guid , field . Name ) )
264289 {
265290 switch ( migResult )
266291 {
0 commit comments