Beispiel #1
0
        public void Seed(SmartObjectContext context)
        {
            context.MigrateLocaleResources(MigrateLocaleResources);

            var mediaStorages  = context.Set <MediaStorage>();
            var fileSystem     = new LocalFileSystem();
            var storeMediaInDb = true;

            {
                var settings = context.Set <Setting>();

                // be careful, this setting does not necessarily exist
                var storeInDbSetting = settings.FirstOrDefault(x => x.Name == "Media.Images.StoreInDB");
                if (storeInDbSetting != null)
                {
                    storeMediaInDb = storeInDbSetting.Value.ToBool(true);

                    // remove old bool StoreInDB because it's not used anymore
                    settings.Remove(storeInDbSetting);
                }

                // set current media storage provider system name
                settings.AddOrUpdate(x => x.Name, new Setting
                {
                    Name  = "Media.Storage.Provider",
                    Value = (storeMediaInDb ? "MediaStorage.SmartStoreDatabase" : "MediaStorage.SmartStoreFileSystem")
                });
            }

            #region Pictures

            if (storeMediaInDb)
            {
                PageEntities(context, mediaStorages, context.Set <Picture>().OrderBy(x => x.Id), picture =>
                {
#pragma warning disable 612, 618
                    if (picture.PictureBinary != null && picture.PictureBinary.LongLength > 0)
                    {
                        var mediaStorage = new MediaStorage {
                            Data = picture.PictureBinary
                        };
                        picture.MediaStorage  = mediaStorage;
                        picture.PictureBinary = null;
                    }
#pragma warning restore 612, 618
                });
            }

            #endregion

            #region Downloads

            PageEntities(context, mediaStorages, context.Set <Download>().OrderBy(x => x.Id), download =>
            {
#pragma warning disable 612, 618
                if (download.DownloadBinary != null && download.DownloadBinary.LongLength > 0)
                {
                    if (storeMediaInDb)
                    {
                        // move binary data
                        var mediaStorage = new MediaStorage {
                            Data = download.DownloadBinary
                        };
                        download.MediaStorage = mediaStorage;
                    }
                    else
                    {
                        // move to file system. it's necessary because from now on DownloadService depends on current storage provider
                        // and it would not find the binary data anymore if do not move it.
                        var fileName = GetFileName(download.Id, download.Extension, download.ContentType);
                        var path     = fileSystem.Combine(@"Media\Downloads", fileName);

                        fileSystem.WriteAllBytes(path, download.DownloadBinary);
                    }

                    download.DownloadBinary = null;
                }
#pragma warning restore 612, 618
            });

            #endregion

            #region Queued email attachments

            var attachmentQuery = context.Set <QueuedEmailAttachment>()
                                  .Where(x => x.StorageLocation == EmailAttachmentStorageLocation.Blob)
                                  .OrderBy(x => x.Id);

            PageEntities(context, mediaStorages, attachmentQuery, attachment =>
            {
#pragma warning disable 612, 618
                if (attachment.Data != null && attachment.Data.LongLength > 0)
                {
                    if (storeMediaInDb)
                    {
                        // move binary data
                        var mediaStorage = new MediaStorage {
                            Data = attachment.Data
                        };
                        attachment.MediaStorage = mediaStorage;
                    }
                    else
                    {
                        // move to file system. it's necessary because from now on QueuedEmailService depends on current storage provider
                        // and it would not find the binary data anymore if do not move it.
                        var fileName = GetFileName(attachment.Id, Path.GetExtension(attachment.Name.EmptyNull()), attachment.MimeType);
                        var path     = fileSystem.Combine(@"Media\QueuedEmailAttachment", fileName);

                        fileSystem.WriteAllBytes(path, attachment.Data);
                    }

                    attachment.Data = null;
                }
#pragma warning restore 612, 618
            });

            #endregion
        }