Exemplo n.º 1
0
        public async Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags)
        {
            var imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());

            if (imageInfo != null)
            {
                command.Type = AssetType.Image;

                command.Metadata.SetPixelWidth(imageInfo.PixelWidth);
                command.Metadata.SetPixelHeight(imageInfo.PixelHeight);

                if (tags != null)
                {
                    tags.Add("image");

                    var wh = imageInfo.PixelWidth + imageInfo.PixelHeight;

                    if (wh > 2000)
                    {
                        tags.Add("image/large");
                    }
                    else if (wh > 1000)
                    {
                        tags.Add("image/medium");
                    }
                    else
                    {
                        tags.Add("image/small");
                    }
                }
            }
        }
Exemplo n.º 2
0
        private const int FileSizeLimit = 2 * 1024 * 1024; // 2MB

        public async Task EnhanceAsync(UploadAssetCommand command)
        {
            var isSvg =
                command.File.MimeType == "image/svg+xml" ||
                command.File.FileName.EndsWith(".svg", StringComparison.OrdinalIgnoreCase);

            if (isSvg)
            {
                command.Tags.Add("image");

                if (command.File.FileSize < FileSizeLimit)
                {
                    try
                    {
                        using (var reader = new StreamReader(command.File.OpenRead()))
                        {
                            var text = await reader.ReadToEndAsync();

                            if (!text.IsValidSvg())
                            {
                                throw new ValidationException(T.Get("validation.notAnValidSvg"));
                            }
                        }
                    }
                    catch (ValidationException)
                    {
                        throw;
                    }
                    catch
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 3
0
 private async Task EnrichWithMetadataAsync(UploadAssetCommand command)
 {
     foreach (var metadataSource in assetMetadataSources)
     {
         await metadataSource.EnhanceAsync(command);
     }
 }
Exemplo n.º 4
0
 private async Task EnrichWithMetadataAsync(UploadAssetCommand command, HashSet <string>?tags)
 {
     foreach (var metadataSource in assetMetadataSources)
     {
         await metadataSource.EnhanceAsync(command, tags);
     }
 }
Exemplo n.º 5
0
        public async Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags)
        {
            if (command.Type == AssetType.Unknown || command.Type == AssetType.Image)
            {
                ImageInfo?imageInfo = null;

                using (var uploadStream = command.File.OpenRead())
                {
                    imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream);
                }

                if (imageInfo != null)
                {
                    var isSwapped = imageInfo.IsRotatedOrSwapped;

                    if (isSwapped)
                    {
                        var tempFile = new TempAssetFile(command.File);

                        using (var uploadStream = command.File.OpenRead())
                        {
                            imageInfo = await assetThumbnailGenerator.FixOrientationAsync(uploadStream, tempFile.Stream);
                        }

                        command.File.Dispose();
                        command.File = tempFile;
                    }

                    if (command.Type == AssetType.Unknown || isSwapped)
                    {
                        command.Type = AssetType.Image;

                        command.Metadata.SetPixelWidth(imageInfo.PixelWidth);
                        command.Metadata.SetPixelHeight(imageInfo.PixelHeight);
                    }
                }
            }

            if (command.Type == AssetType.Image && tags != null)
            {
                tags.Add("image");

                var wh = command.Metadata.GetPixelWidth() + command.Metadata.GetPixelWidth();

                if (wh > 2000)
                {
                    tags.Add("image/large");
                }
                else if (wh > 1000)
                {
                    tags.Add("image/medium");
                }
                else
                {
                    tags.Add("image/small");
                }
            }
        }
Exemplo n.º 6
0
        private async Task EnrichWithHashAndUploadAsync(UploadAssetCommand command, string tempFile)
        {
            using (var hashStream = new HasherStream(command.File.OpenRead(), HashAlgorithmName.SHA256))
            {
                await assetFileStore.UploadAsync(tempFile, hashStream);

                command.FileHash = $"{hashStream.GetHashStringAndReset()}{command.File.FileName}{command.File.FileSize}".Sha256Base64();
            }
        }
Exemplo n.º 7
0
        private async Task UploadAsync(CommandContext context, string tempFile, UploadAssetCommand command, HashSet <string>?tags, bool created, NextDelegate next)
        {
            await EnrichWithMetadataAsync(command, tags);

            var asset = await HandleCoreAsync(context, created, next);

            if (asset != null)
            {
                await assetFileStore.CopyAsync(tempFile, command.AppId.Id, command.AssetId, asset.FileVersion);
            }
        }
Exemplo n.º 8
0
        private async Task EnrichWithHashAndUploadAsync(UploadAssetCommand command, string tempFile)
        {
            using (var uploadStream = command.File.OpenRead())
            {
                using (var hashStream = new HasherStream(uploadStream, HashAlgorithmName.SHA256))
                {
                    await assetFileStore.UploadAsync(tempFile, hashStream);

                    command.FileHash = ComputeHash(command.File, hashStream);
                }
            }
        }
Exemplo n.º 9
0
        public Task EnhanceAsync(UploadAssetCommand command)
        {
            if (command.Tags != null)
            {
                var extension = command.File?.FileName?.FileType();

                if (!string.IsNullOrWhiteSpace(extension))
                {
                    command.Tags.Add($"type/{extension.ToLowerInvariant()}");
                }
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 10
0
        public Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags)
        {
            if (tags != null)
            {
                var extension = command.File?.FileName?.FileType();

                if (!string.IsNullOrWhiteSpace(extension))
                {
                    tags.Add($"type/{extension.ToLowerInvariant()}");
                }
            }

            return(TaskHelper.Done);
        }
Exemplo n.º 11
0
        private async Task UploadAndHandleAsync(CommandContext context, UploadAssetCommand command, NextDelegate next)
        {
            var tempFile = context.ContextId.ToString();

            try
            {
                await EnrichWithHashAndUploadAsync(command, tempFile);
                await EnrichWithMetadataAsync(command);

                await base.HandleAsync(context, next);
            }
            finally
            {
                await assetFileStore.DeleteAsync(tempFile);

                await command.File.DisposeAsync();
            }
        }
Exemplo n.º 12
0
        public async Task EnhanceAsync(UploadAssetCommand command)
        {
            try
            {
                if (command.Type == AssetType.Image && command.File.FileSize <= MaxSize)
                {
                    await using (var stream = command.File.OpenRead())
                    {
                        var result = await client.AnalyzeImageInStreamAsync(stream, features);

                        command.Tags ??= new HashSet <string>();

                        if (result.Color?.DominantColorForeground != null)
                        {
                            command.Tags.Add($"color/{result.Color.DominantColorForeground.Trim(trimChars).ToLowerInvariant()}");
                        }

                        if (result.Categories != null)
                        {
                            foreach (var category in result.Categories.OrderByDescending(x => x.Score).Take(3))
                            {
                                command.Tags.Add($"category/{category.Name.Trim(trimChars).ToLowerInvariant()}");
                            }
                        }

                        var description = result.Description?.Captions?.OrderByDescending(x => x.Confidence)?.FirstOrDefault()?.Text;

                        if (description != null)
                        {
                            command.Metadata["caption"] = JsonValue.Create(description);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "EnrichWithAzure")
                             .WriteProperty("status", "Failed"));
            }
        }
Exemplo n.º 13
0
        private async Task UploadWithDuplicateCheckAsync(CommandContext context, UploadAssetCommand command, bool duplicate, NextDelegate next)
        {
            var tempFile = context.ContextId.ToString();

            try
            {
                await EnrichWithHashAndUploadAsync(command, tempFile);

                if (!duplicate)
                {
                    var existing =
                        await assetQuery.FindByHashAsync(contextProvider.Context,
                                                         command.FileHash,
                                                         command.File.FileName,
                                                         command.File.FileSize);

                    if (existing != null)
                    {
                        context.Complete(new AssetDuplicate(existing));

                        await next(context);

                        return;
                    }
                }

                await EnrichWithMetadataAsync(command);

                await base.HandleAsync(context, next);
            }
            finally
            {
                await assetFileStore.DeleteAsync(tempFile);

                await command.File.DisposeAsync();
            }
        }
Exemplo n.º 14
0
        public async Task EnhanceAsync(UploadAssetCommand command)
        {
            if (command.Type == AssetType.Unknown || command.Type == AssetType.Image)
            {
                var mimeType = command.File.MimeType;

                ImageInfo?imageInfo = null;

                await using (var uploadStream = command.File.OpenRead())
                {
                    imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream, mimeType);
                }

                if (imageInfo != null)
                {
                    var isSwapped = imageInfo.Orientation > ImageOrientation.TopLeft;

                    if (command.File != null && isSwapped)
                    {
                        var tempFile = TempAssetFile.Create(command.File);

                        await using (var uploadStream = command.File.OpenRead())
                        {
                            await using (var tempStream = tempFile.OpenWrite())
                            {
                                await assetThumbnailGenerator.FixOrientationAsync(uploadStream, mimeType, tempStream);
                            }
                        }

                        await using (var tempStream = tempFile.OpenRead())
                        {
                            imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(tempStream, mimeType) ?? imageInfo;
                        }

                        await command.File.DisposeAsync();

                        command.File = tempFile;
                    }

                    if (command.Type == AssetType.Unknown || isSwapped)
                    {
                        command.Type = AssetType.Image;

                        command.Metadata.SetPixelWidth(imageInfo.PixelWidth);
                        command.Metadata.SetPixelHeight(imageInfo.PixelHeight);
                    }
                }
            }

            if (command.Tags == null)
            {
                return;
            }

            if (command.Type == AssetType.Image)
            {
                command.Tags.Add("image");

                var wh = command.Metadata.GetPixelWidth() + command.Metadata.GetPixelWidth();

                if (wh > 2000)
                {
                    command.Tags.Add("image/large");
                }
                else if (wh > 1000)
                {
                    command.Tags.Add("image/medium");
                }
                else
                {
                    command.Tags.Add("image/small");
                }
            }
        }
Exemplo n.º 15
0
        public Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags)
        {
            Enhance(command, tags);

            return(TaskHelper.Done);
        }
Exemplo n.º 16
0
 private async Task EnrichWithImageInfosAsync(UploadAssetCommand command)
 {
     command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());
 }
Exemplo n.º 17
0
        private void Enhance(UploadAssetCommand command)
        {
            try
            {
                using (var file = Create(new FileAbstraction(command.File), ReadStyle.Average))
                {
                    if (file.Properties == null)
                    {
                        return;
                    }

                    var type = file.Properties.MediaTypes;

                    if (type == MediaTypes.Audio)
                    {
                        command.Type = AssetType.Audio;
                    }
                    else if (type == MediaTypes.Photo)
                    {
                        command.Type = AssetType.Image;
                    }
                    else if (type.HasFlag(MediaTypes.Video))
                    {
                        command.Type = AssetType.Video;
                    }

                    var pw = file.Properties.PhotoWidth;
                    var ph = file.Properties.PhotoHeight;

                    if (pw > 0 && pw > 0)
                    {
                        command.Metadata.SetPixelWidth(pw);
                        command.Metadata.SetPixelHeight(ph);
                    }

                    void TryAddString(string name, string?value)
                    {
                        if (!string.IsNullOrWhiteSpace(value))
                        {
                            command.Metadata.Add(name, JsonValue.Create(value));
                        }
                    }

                    void TryAddInt(string name, int?value)
                    {
                        if (value > 0)
                        {
                            command.Metadata.Add(name, JsonValue.Create(value));
                        }
                    }

                    void TryAddDouble(string name, double?value)
                    {
                        if (value > 0)
                        {
                            command.Metadata.Add(name, JsonValue.Create(value));
                        }
                    }

                    void TryAddTimeSpan(string name, TimeSpan value)
                    {
                        if (value != TimeSpan.Zero)
                        {
                            command.Metadata.Add(name, JsonValue.Create(value.ToString()));
                        }
                    }

                    if (file.Tag is ImageTag imageTag)
                    {
                        TryAddDouble("latitude", imageTag.Latitude);
                        TryAddDouble("longitude", imageTag.Longitude);

                        TryAddString("created", imageTag.DateTime?.ToIso8601());
                    }

                    TryAddTimeSpan("duration", file.Properties.Duration);

                    TryAddInt("audioBitrate", file.Properties.AudioBitrate);
                    TryAddInt("audioChannels", file.Properties.AudioChannels);
                    TryAddInt("audioSampleRate", file.Properties.AudioSampleRate);
                    TryAddInt("bitsPerSample", file.Properties.BitsPerSample);
                    TryAddInt("imageQuality", file.Properties.PhotoQuality);
                    TryAddInt("videoWidth", file.Properties.VideoWidth);
                    TryAddInt("videoHeight", file.Properties.VideoHeight);

                    TryAddString("description", file.Properties.Description);
                }
            }
            catch
            {
                return;
            }
        }
Exemplo n.º 18
0
        public Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags)
        {
            Enhance(command);

            return(Task.CompletedTask);
        }