Ejemplo n.º 1
0
        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 }))
            });
        }
Ejemplo n.º 2
0
        protected override BatchJobExecutionResult OnExecute(MigrateFilesBatchJob batchJob)
        {
            List <Guid> guids = JsonConvert.DeserializeObject <HashSet <Guid> >(batchJob.Data).ToList();

            IList <MediaFile> mediaFiles = _session.QueryOver <MediaFile>().Where(x => x.Guid.IsIn(guids)).List();


            foreach (MediaFile mediaFile in mediaFiles)
            {
                IFileSystem from = MediaFileExtensions.GetFileSystem(mediaFile.FileUrl, _fileSystems);
                IFileSystem to   = CurrentFileSystem;
                if (from.GetType() == to.GetType())
                {
                    continue;
                }

                _session.Transact(session =>
                {
                    // remove resized images (they will be regenerated on the to system)
                    foreach (ResizedImage 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);
                    }

                    string existingUrl = mediaFile.FileUrl;
                    using (System.IO.Stream readStream = @from.GetReadStream(existingUrl))
                    {
                        mediaFile.FileUrl = to.SaveFile(readStream, GetNewFilePath(mediaFile),
                                                        mediaFile.ContentType);
                    }
                    from.Delete(existingUrl);

                    session.Update(mediaFile);
                });
            }


            return(BatchJobExecutionResult.Success());
        }