Ejemplo n.º 1
0
        public async Task <IResult> Post([Parameter(ParameterSource.Url)] string collection)
        {
            if (string.IsNullOrWhiteSpace(Context.Request.UserID))
            {
                return(Forbidden);
            }

            if (string.IsNullOrWhiteSpace(collection))
            {
                return(BadRequest);
            }

            if (Context.Request.Files.Count != 1)
            {
                return(BadRequest);
            }

            using (var img = Image.Load(Context.Request.Files.First().Data, out var format))
                using (var sha256 = System.Security.Cryptography.SHA256.Create())
                {
                    img.Metadata.ExifProfile = null;
                    Directory.CreateDirectory(StoragePath);

                    var path = Path.Combine(
                        StoragePath,
                        Path.ChangeExtension(
                            Guid.NewGuid().ToString(),
                            format.FileExtensions.FirstOrDefault()
                            )
                        );

                    var enc = ImageHandler.GetEncoder(format.Name);
                    img.Save(path, enc);
                    string etag;
                    using (var fs = File.OpenRead(path))
                        etag = Convert.ToBase64String(sha256.ComputeHash(fs));

                    var mapentry = new Database.ImageMap()
                    {
                        UserID       = Context.Request.UserID,
                        CollectionID = collection,
                        Path         = path,
                        Width        = img.Width,
                        Height       = img.Height,
                        ContentType  = format.DefaultMimeType,
                        Sha256       = etag
                    };

                    await DB.RunInTransactionAsync(db => db.InsertItem(mapentry));

                    if (collection == "front-page")
                    {
                        await Task.Run(() => Cache.InvalidateMainIndexHtmlAsync());
                    }

                    return(Json(new { ID = mapentry.ID }));
                }
        }
Ejemplo n.º 2
0
 public ResultEntry(Database.ImageMap map)
 {
     this.ID      = map.ID;
     this.Size    = FileSize(map.Path);
     this.Created = map.Created;
 }