コード例 #1
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.");
        }
コード例 #2
0
        // DUPLICATED CODE FROM ProfileController.cs


        private async Task <IActionResult> CreateAsync <T>(ShareModel model, Func <string, IQueryable <T> > findQuery, Func <T> entityFactory)
            where T : ShareBase
        {
            string userId;

            if (model.UserName != null && model.UserName != ShareStatusService.PublicUserName)
            {
                userId = (await userNames.GetUserIdsAsync(new[] { model.UserName })).First();
            }
            else
            {
                userId = ShareStatusService.PublicUserId;
            }

            if (userId == ShareStatusService.PublicUserId && model.Permission != Permission.Read)
            {
                return(BadRequest());
            }

            T entity = await findQuery(userId).FirstOrDefaultAsync();

            if (entity == null)
            {
                entity            = entityFactory();
                entity.UserId     = userId;
                entity.Permission = (int)model.Permission;

                await db.Set <T>().AddAsync(entity);
            }
            else
            {
                entity.Permission = (int)model.Permission;
            }

            await db.SaveChangesAsync();

            return(StatusCode(StatusCodes.Status201Created));
        }
コード例 #3
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();
                }
        }