<commit_before>﻿using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using JabbR.ContentProviders.Core;
using JabbR.Infrastructure;
using JabbR.Services;
using JabbR.UploadHandlers;
using Microsoft.Security.Application;
using Ninject;

namespace JabbR.ContentProviders
{
    public class ImageContentProvider : CollapsibleContentProvider
    {
        private readonly IKernel _kernel;
        private readonly IJabbrConfiguration _configuration;

        [ImportingConstructor]
        public ImageContentProvider(IKernel kernel)
        {
            _kernel = kernel;
            _configuration = kernel.Get<IJabbrConfiguration>();
        }

        protected override async Task<ContentProviderResult> GetCollapsibleContent(ContentProviderHttpRequest request)
        {
            string format = @"<a rel=""nofollow external"" target=""_blank"" href=""{0}""><img src=""{1}"" /></a>";
            string imageUrl = request.RequestUri.ToString();
            string href = imageUrl;

            Trace.TraceInformation("Processing image url " + imageUrl + ".");

            // Only proxy what we need to (non https images)
            if (_configuration.RequireHttps &&
                !request.RequestUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
            {
                Trace.TraceInformation("Proxying of image url " + imageUrl + " is required.");

                try
                {
                    var uploadProcessor = _kernel.Get<UploadProcessor>();
                    var response = await Http.GetAsync(request.RequestUri);
                    string fileName = Path.GetFileName(request.RequestUri.LocalPath);
                    string contentType = GetContentType(request.RequestUri);
                    long contentLength = response.ContentLength;

                    using (Stream stream = response.GetResponseStream())
                    {
                        Trace.TraceInformation("Uploading content " + imageUrl + ".");

                        UploadResult result = await uploadProcessor.HandleUpload(fileName, contentType, stream, contentLength);

                        Trace.TraceInformation("Uploading " + imageUrl + " complete.");

                        if (result != null)
                        {
                            imageUrl = result.Url;
                        }
                        else
                        {
                            Trace.TraceError("Failed to upload to blob storage. The upload result was null.");
                        }
                    }
                }
                catch (Exception ex)
                {<commit_msg>Added more tracing to image url processing<commit_after>﻿using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using JabbR.ContentProviders.Core;
using JabbR.Infrastructure;
using JabbR.Services;
using JabbR.UploadHandlers;
using Microsoft.Security.Application;
using Ninject;

namespace JabbR.ContentProviders
{
    public class ImageContentProvider : CollapsibleContentProvider
    {
        private readonly IKernel _kernel;
        private readonly IJabbrConfiguration _configuration;

        [ImportingConstructor]
        public ImageContentProvider(IKernel kernel)
        {
            _kernel = kernel;
            _configuration = kernel.Get<IJabbrConfiguration>();
        }

        protected override async Task<ContentProviderResult> GetCollapsibleContent(ContentProviderHttpRequest request)
        {
            string format = @"<a rel=""nofollow external"" target=""_blank"" href=""{0}""><img src=""{1}"" /></a>";
            string imageUrl = request.RequestUri.ToString();
            string href = imageUrl;

            Trace.TraceInformation("Processing image url " + imageUrl + ".");

            // Only proxy what we need to (non https images)
            if (_configuration.RequireHttps &&
                !request.RequestUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
            {
                Trace.TraceInformation("Proxying of image url " + imageUrl + " is required.");

                try
                {
                    var uploadProcessor = _kernel.Get<UploadProcessor>();

                    Trace.TraceInformation("Http.GetAsync(" + request.RequestUri + ")");

                    var response = await Http.GetAsync(request.RequestUri);
                    string fileName = Path.GetFileName(request.RequestUri.LocalPath);
                    string contentType = GetContentType(request.RequestUri);
                    long contentLength = response.ContentLength;

                    Trace.TraceInformation("Status code: " + response.StatusCode);
                    Trace.TraceInformation("response.GetResponseStream()");

                    using (Stream stream = response.GetResponseStream())
                    {
                        Trace.TraceInformation("Uploading content " + imageUrl + ".");

                        UploadResult result = await uploadProcessor.HandleUpload(fileName, contentType, stream, contentLength);

                        Trace.TraceInformation("Uploading " + imageUrl + " complete.");

                        if (result != null)
                        {
                            imageUrl = result.Url;
                        }
                        else
                        {
                            Trace.TraceError("Failed to upload to blob storage. The upload result was null.");
                        }
                    }
                }
                catch (Exception ex)
                {
{'fraction_non_alphanumeric': 0.08636363636363636, 'fraction_numerical': 0.0006993006993006993, 'mean_word_length': 1.8154527559055118, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4220863', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1229, 'n_words': 298}