public FileMigrationResult MigrateFiles() { IList <MediaFile> mediaFiles = _session.QueryOver <MediaFile>().List(); List <Guid> guids = mediaFiles.Where( mediaFile => MediaFileExtensions.GetFileSystem(mediaFile.FileUrl, _allFileSystems.Values) != CurrentFileSystem) .Select(file => file.Guid).ToList(); if (!guids.Any()) { return(new FileMigrationResult { MigrationRequired = false, Message = "Migration not required" }); } BatchCreationResult result = _createBatch.Create(guids.Chunk(10) .Select(set => new MigrateFilesBatchJob { Data = JsonConvert.SerializeObject(set.ToHashSet()), })); return(new FileMigrationResult { MigrationRequired = true, Message = string.Format( "Batch created. Click <a target=\"_blank\" href=\"{0}\">here</a> to view and start.".AsResource( _kernel), _urlHelper.Action("Show", "BatchRun", new { id = result.InitialBatchRun.Id })) }); }
protected override BatchJobExecutionResult OnExecute(MigrateFilesBatchJob batchJob) { var guids = JsonConvert.DeserializeObject <HashSet <Guid> >(batchJob.Data).ToList(); var mediaFiles = _session.QueryOver <MediaFile>().Where(x => x.Guid.IsIn(guids)).List(); foreach (var mediaFile in mediaFiles) { var from = MediaFileExtensions.GetFileSystem(mediaFile.FileUrl, _fileSystems); var to = CurrentFileSystem; if (from.GetType() == to.GetType()) { continue; } _session.Transact(session => { // remove resized images (they will be regenerated on the to system) foreach (var resizedImage in mediaFile.ResizedImages.ToList()) { // check for resized file having same url as the original - // do not delete from disc yet in that case, or else it will cause an error when copying if (resizedImage.Url != mediaFile.FileUrl) { from.Delete(resizedImage.Url); } mediaFile.ResizedImages.Remove(resizedImage); session.Delete(resizedImage); } var existingUrl = mediaFile.FileUrl; using (var readStream = @from.GetReadStream(existingUrl)) { mediaFile.FileUrl = to.SaveFile(readStream, GetNewFilePath(mediaFile), mediaFile.ContentType); } from.Delete(existingUrl); session.Update(mediaFile); }); } return(BatchJobExecutionResult.Success()); }