Exemplo n.º 1
0
        private static async Task <bool> TryUpdateOriginalSizeAsync(EntriesDataContext entries, IFileStorage fileStorage, ImageResizeService resizeService, Image image, ImageType imageType)
        {
            var fileContent = await fileStorage.FindAsync(image.Entry, image, imageType);

            if (fileContent != null)
            {
                using (fileContent)
                {
                    var size = resizeService.GetSize(fileContent);
                    if (size.width != image.OriginalWidth || size.height != image.OriginalHeight)
                    {
                        image.OriginalWidth  = size.width;
                        image.OriginalHeight = size.height;

                        entries.Images.Update(image);
                    }
                }

                return(true);
            }
            else
            {
                Console.WriteLine($"Missing '{imageType}' file for '{image.Id}'.");
                return(false);
            }
        }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Pass first argument with connection string to database, second with file storage type (fs/azure), third a connection to azure storage or local path template (relative path not supported).");
                return;
            }

            string connectionString = args[0];

            Console.WriteLine("Creating context.");
            using var entries = new EntriesDataContext(DbContextOptions <EntriesDataContext>(connectionString, "Entries"), Schema <EntriesDataContext>("Entries"));

            var imageFormat = ImageFormatDefinition.Jpeg;

            IFileStorage fileStorage = null;
            string       storageType = args[1];

            if (storageType == "fs")
            {
                fileStorage = new SystemIoFileStorage(path => path, Options.Create(new SystemIoStorageOptions()
                {
                    PathTemplate = args[2]
                }), imageFormat);
            }
            else if (storageType == "azure")
            {
                fileStorage = new AzureFileStorage(Options.Create(new AzureStorageOptions()
                {
                    ConnectionString = args[2]
                }));
            }
            else
            {
                Console.WriteLine($"Not supported type of file storage '{storageType}'.");
                return;
            }

            var resizeService = new ImageResizeService(imageFormat);

            Console.WriteLine("Getting images.");
            var images = await entries.Images
                         .Include(i => i.Entry)
                         //.Where(i => i.OriginalWidth == 0 || i.OriginalHeight == 0)
                         .ToListAsync();

            Console.WriteLine($"Found '{images.Count}' images.");
            foreach (var image in images)
            {
                if (!await TryUpdateOriginalSizeAsync(entries, fileStorage, resizeService, image, ImageType.Original))
                {
                    await TryUpdateOriginalSizeAsync(entries, fileStorage, resizeService, image, ImageType.Preview);
                }
            }

            Console.WriteLine("Saving changes.");
            await entries.SaveChangesAsync();

            Console.WriteLine("Done.");
        }
Exemplo n.º 3
0
 public ShareController(DataContext db, IUserNameProvider userNames, ShareStatusService shareStatus)
     : base(db, shareStatus)
 {
     Ensure.NotNull(db, "db");
     Ensure.NotNull(userNames, "userNames");
     this.db        = db;
     this.userNames = userNames;
 }
Exemplo n.º 4
0
        private async static Task MigrateEntriesAsync(string sourceConnectionString, string targetConnectionString)
        {
            using (var source = new EntriesDataContext(new DbContextOptionsBuilder <EntriesDataContext>().UseSqlite(sourceConnectionString).Options, new SchemaOptions <EntriesDataContext>()))
                using (var target = new EntriesDataContext(new DbContextOptionsBuilder <EntriesDataContext>().UseSqlServer(targetConnectionString).Options, new SchemaOptions <EntriesDataContext>()
                {
                    Name = "Entries"
                }))
                {
                    await CopyDbSetAsync(source, target, c => c.Entries);
                    await CopyDbSetAsync(source, target, c => c.Images, image => target.Entry(image.Location).State = EntityState.Added);
                    await CopyDbSetAsync(source, target, c => c.Stories, story =>
                    {
                        foreach (var chapter in story.Chapters)
                        {
                            if (target.Entry(chapter).State != EntityState.Unchanged && target.Entry(chapter).State != EntityState.Modified)
                            {
                                target.Entry(chapter).State = EntityState.Added;
                            }
                        }
                    }, nameof(Story.Chapters));

                    await target.SaveChangesAsync();
                }
        }