<commit_before>using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AnlabMvc.Models.FileUploadModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Shared.Protocol;

namespace AnlabMvc.Services
{
    public interface IFileStorageService
    {
        /// <summary>
        /// Returns a unique URL, including Shared Signature, which can be used to upload the file given by fileName
        /// Only valid for a few minutes
        /// </summary>
        /// <param name="fileName">file name with extension</param>
        /// <returns></returns>
        Task<SasResponse> GetSharedAccessSignature(string fileName);

        /// <summary>
        /// uploads a number of files to blob storage
        /// </summary>
        Task UploadFiles(params FileUpload[] files);

        string GetFullUriFromIdentifier(string identifier);

        Task<string> UploadFile(IFormFile file);
    }

    public class FileStorageService : IFileStorageService
    {
        private readonly AppSettings _appSettings;
        //private readonly string _containerName;
        private readonly ConnectionSettings _connectionSetting;

        public FileStorageService(IOptions<ConnectionSettings> connectionSetting, IOptions<AppSettings> appSettings)
        {
            _appSettings = appSettings.Value;
            _connectionSetting = connectionSetting.Value;
            //_containerName = _appSettings.StorageContainerName;
        }


        public async Task<SasResponse> GetSharedAccessSignature(string fileName)
        {
            string storageConnectionString = _connectionSetting.StorageConnection;

            // Create the storage account with the connection string.
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);

            // Create the blob client object.
            var blobClient = storageAccount.CreateCloudBlobClient();

            var props = await blobClient.GetServicePropertiesAsync();

            if (!props.Cors.CorsRules.Any())
            {
                //create the default cors access rule
                var cors = new CorsRule();
                cors.AllowedHeaders.Add("*");
                cors.AllowedMethods = CorsHttpMethods.Get | CorsHttpMethods.Put;
                cors.AllowedOrigins.Add("*"); //TODO: only allow certain origins
                cors.ExposedHeaders.Add("x-ms-*");
                cors.MaxAgeInSeconds = 60 * 60 * 24 * 365; //one year
                props.Cors.CorsRules.Clear();
                props.Cors.CorsRules.Add(cors);<commit_msg>so Scott isn't sad<commit_after>using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AnlabMvc.Models.FileUploadModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Shared.Protocol;

namespace AnlabMvc.Services
{
    public interface IFileStorageService
    {
        /// <summary>
        /// Returns a unique URL, including Shared Signature, which can be used to upload the file given by fileName
        /// Only valid for a few minutes
        /// </summary>
        /// <param name="fileName">file name with extension</param>
        /// <returns></returns>
        Task<SasResponse> GetSharedAccessSignature(string fileName);

        /// <summary>
        /// uploads a number of files to blob storage
        /// </summary>
        Task UploadFiles(params FileUpload[] files);

        string GetFullUriFromIdentifier(string identifier);

        Task<string> UploadFile(IFormFile file);
    }

    public class FileStorageService : IFileStorageService
    {
        private readonly AppSettings _appSettings;
        private readonly ConnectionSettings _connectionSetting;

        public FileStorageService(IOptions<ConnectionSettings> connectionSetting, IOptions<AppSettings> appSettings)
        {
            _appSettings = appSettings.Value;
            _connectionSetting = connectionSetting.Value;
        }


        public async Task<SasResponse> GetSharedAccessSignature(string fileName)
        {
            string storageConnectionString = _connectionSetting.StorageConnection;

            // Create the storage account with the connection string.
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);

            // Create the blob client object.
            var blobClient = storageAccount.CreateCloudBlobClient();

            var props = await blobClient.GetServicePropertiesAsync();

            if (!props.Cors.CorsRules.Any())
            {
                //create the default cors access rule
                var cors = new CorsRule();
                cors.AllowedHeaders.Add("*");
                cors.AllowedMethods = CorsHttpMethods.Get | CorsHttpMethods.Put;
                cors.AllowedOrigins.Add("*"); //TODO: only allow certain origins
                cors.ExposedHeaders.Add("x-ms-*");
                cors.MaxAgeInSeconds = 60 * 60 * 24 * 365; //one year
                props.Cors.CorsRules.Clear();
                props.Cors.CorsRules.Add(cors);
{'fraction_non_alphanumeric': 0.08607547871351552, 'fraction_numerical': 0.0033463469046291134, 'mean_word_length': 2.520942408376963, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '3941139', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1295, 'n_words': 340}