Example #1
0
        public override async Task <FileData> GetAsync(FileGetOptions fileGetOptions)
        {
            FileData file = new FileData();

            CloudStorageAccount storageAccount = Authorized(fileGetOptions);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare fileshare = fileClient.GetShareReference(fileGetOptions.Folder);

            if (fileshare.ExistsAsync().Result)
            {
                CloudFileDirectory cFileDir = fileshare.GetRootDirectoryReference();

                CloudFile cFile = cFileDir.GetFileReference(fileGetOptions.Key);

                if (cFile.ExistsAsync().Result)
                {
                    file.Stream.Position = 0;
                    await cFile.DownloadToStreamAsync(file.Stream);
                }
            }

            file.Type = "Azure File Storage";

            return(file);
        }
Example #2
0
        private static async void GetListOfFilesSegmented()
        {
            var            client         = cloudStorageAccount.CreateCloudFileClient();
            CloudFileShare cloudFileShare = client.GetShareReference("trial");

            if (cloudFileShare != null && await cloudFileShare.ExistsAsync())
            {
                CloudFileDirectory cloudFileDirectory = cloudFileShare.GetRootDirectoryReference();
                if (cloudFileDirectory != null && await cloudFileDirectory.ExistsAsync())
                {
                    FileContinuationToken fileContinuationToken = new FileContinuationToken();

                    do
                    {
                        FileResultSegment fileResultSegment = await cloudFileDirectory.ListFilesAndDirectoriesSegmentedAsync(fileContinuationToken);

                        foreach (CloudFile result in fileResultSegment.Results)
                        {
                            Console.WriteLine($"{result.Name} - {result.Properties.Length} bytes");
                        }
                        fileContinuationToken = fileResultSegment.ContinuationToken;
                    } while (fileContinuationToken != null);
                }
                else
                {
                    Console.WriteLine("Cloud file directory is null");
                }
            }
            else
            {
                Console.WriteLine("Cloud file share is null");
            }
        }
Example #3
0
        /// <summary>
        /// DownloadFileAsyncStream
        /// </summary>
        /// <param name="shareName"></param>
        /// <param name="sourceFolder"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task <MemoryStream> DownloadFileAsyncStream(string shareName, string sourceFolder, string fileName)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(settings.ConnectionString);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare fileShare = fileClient.GetShareReference(shareName);

            if (await fileShare.ExistsAsync())
            {
                CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();
                if (await rootDirectory.ExistsAsync())
                {
                    CloudFileDirectory customDirectory = rootDirectory.GetDirectoryReference(sourceFolder);
                    if (await customDirectory.ExistsAsync())
                    {
                        CloudFile cloudfile = customDirectory.GetFileReference(fileName);
                        if (await cloudfile.ExistsAsync())
                        {
                            using (var stream = new MemoryStream())
                            {
                                await cloudfile.DownloadToStreamAsync(stream);

                                return(stream);
                            }
                        }
                    }
                }
            }
            return(null);
        }
Example #4
0
        private static async Task refineLogs()
        {
            StorageConnection conn             = new StorageConnection();
            string            connectionString = "DefaultEndpointsProtocol=https;AccountName=storageaccountgepte86f5;AccountKey=tWMNBa2qlEgVEt6cOnmDbYdsJ0igQmnmJzcJx2d5lxuf0y1iYyMEbkM9n8KlUfPvlSF9Mtc3KE5CrhAWy/fpAg==;EndpointSuffix=core.windows.net";

            conn.config = new MyConfig()
            {
                StorageConnection = connectionString, Container = "techathoninput"
            };
            conn.Connect();
            CloudStorageAccount sAccount   = conn.storageAccount;
            CloudFileClient     fileClient = sAccount.CreateCloudFileClient();

            CloudFileShare share = fileClient.GetShareReference("scheduledfileconverter91d0");

            if (await share.ExistsAsync())
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                CloudFileDirectory dir     = rootDir.GetDirectoryReference(@"LogFiles/Application/Functions/Function/Function");
                CloudFile          logs    = rootDir.GetFileReference("Logs.txt");
                if (await dir.ExistsAsync())
                {
                    List <IListFileItem>  results = new List <IListFileItem>();
                    FileContinuationToken token   = null;
                    do
                    {
                        FileResultSegment resultSegment = await dir.ListFilesAndDirectoriesSegmentedAsync(token);

                        results.AddRange(resultSegment.Results);
                        token = resultSegment.ContinuationToken;
                    }while (token != null);
                    List <CloudFile> files = new List <CloudFile>();
                    foreach (IListFileItem item in results)
                    {
                        if (item.GetType() == typeof(Microsoft.WindowsAzure.Storage.File.CloudFile))
                        {
                            files.Add((CloudFile)item);
                        }
                    }
                    string name = "";
                    foreach (CloudFile file in files)
                    {
                        name = (string.Compare(name, Path.GetFileNameWithoutExtension(file.Name)) > 0) ? name : Path.GetFileNameWithoutExtension(file.Name);
                    }
                    name += ".log";
                    CloudFile recentFile = dir.GetFileReference(name);
                    string    data       = await recentFile.DownloadTextAsync();

                    string[] lines = data.Split("\n");
                    if (lines.Length < 50)
                    {
                        await logs.UploadTextAsync(data);
                    }
                    else
                    {
                        await logs.UploadTextAsync(string.Join("\n", lines.Skip(lines.Length - 50).ToArray()));
                    }
                }
            }
        }
Example #5
0
 private async Task CheckFileShare()
 {
     if (!(await _fileShare.ExistsAsync()))
     {
         throw new ArgumentException($"FileShare {_fileShareName} não existe.");
     }
 }
Example #6
0
        /// <summary>
        /// DownloadFileAsync which using shareNname, sourcefoldername, file namd and filedownload path
        /// </summary>
        /// <param name="shareName"></param>
        /// <param name="sourceFolder"></param>
        /// <param name="fileName"></param>
        /// <param name="fileDownloadPath"></param>
        /// <returns></returns>
        public async Task DownloadFileAsync(string shareName, string sourceFolder, string fileName, string fileDownloadPath)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(settings.ConnectionString);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare fileShare = fileClient.GetShareReference(shareName);

            if (await fileShare.ExistsAsync())
            {
                CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();
                if (await rootDirectory.ExistsAsync())
                {
                    CloudFileDirectory customDirectory = rootDirectory.GetDirectoryReference(sourceFolder);
                    if (await customDirectory.ExistsAsync())
                    {
                        CloudFile cloudfile = customDirectory.GetFileReference(fileName);
                        if (await cloudfile.ExistsAsync())
                        {
                            await cloudfile.DownloadToFileAsync(fileDownloadPath, System.IO.FileMode.OpenOrCreate);
                        }
                    }
                }
            }
        }
        public async static Task <bool> RemoveFileFromCloudFile(string connectionString, string fileFolder, string filename)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                connectionString);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            // Get a reference to the file share we created previously.
            CloudFileShare     share   = fileClient.GetShareReference(fileFolder);
            CloudFileDirectory userDir = null;

            // Ensure that the share exists.
            if (await share.ExistsAsync())
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                // Get a reference to the directory we created previously.
                userDir = rootDir.GetDirectoryReference("audio");
                // Ensure that the directory exists.
                if (await userDir.ExistsAsync())
                {
                    CloudFile removefile = userDir.GetFileReference(filename);
                    await removefile.DeleteIfExistsAsync();

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Example #8
0
        public static async void RunAsync([TimerTrigger("* */1 * * * *")] TimerInfo myTimer, ILogger log, ExecutionContext context)
        {
            //log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            var connStr = Utils.Utility.GetConfigurationItem(context, "Storage_Connection_String");

            var queueName     = Utils.Utility.GetConfigurationItem(context, "Queue-Name");
            var fileShareName = Utils.Utility.GetConfigurationItem(context, "Share-Out");


            string directoryName = null;
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connStr);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  fileShare  = fileClient.GetShareReference(fileShareName); //Get a reference to the passed fileShare
            //Get a reference to the passed Directory
            CloudFileDirectory shareDirectory;

            if (String.IsNullOrEmpty(directoryName))
            {
                shareDirectory = fileShare.GetRootDirectoryReference();
            }
            else
            {
                shareDirectory = fileShare.GetRootDirectoryReference().GetDirectoryReference(directoryName);
            }

            List <IListFileItem> lstFiles = new List <IListFileItem>();

            FileContinuationToken token = null;

            if (fileShare.ExistsAsync().Result)
            {
                do
                {
                    FileResultSegment resultSegment = await shareDirectory.ListFilesAndDirectoriesSegmentedAsync(token);

                    lstFiles.AddRange(resultSegment.Results);
                    token = resultSegment.ContinuationToken;
                } while (token != null);
            }

            foreach (var file in lstFiles)
            {
                await addFileToQueueAsync(queueName, file.Uri.ToString(), connStr);

                string filename = System.IO.Path.GetFileName(file.Uri.LocalPath);
                log.LogInformation($"Queued File: {filename}");
                int statusCode = await AzTable.TableLogger.writeToTable(filename, "Azure Files", Utils.Utility.NextHop.Queue, context);

                if (statusCode >= 200)
                {
                    log.LogInformation("File Motion logged to Table");
                }
            }
        }
        public async Task Test_Azure_File_Storage_Connect()
        {
            Check.That(this._storageAccountName).IsNotEmpty();

            Check.That(this._storageKey).IsNotEmpty();

            CloudFileShare sharedFile = this._cloudFileClient.GetShareReference("demofileshare01");

            bool result = await sharedFile.ExistsAsync(); //checks whether is exists

            Check.That(result).IsTrue();
        }
        private async Task <CloudFileShare> GetOrCreateShareAsync(CloudStorageAccount account)
        {
            CloudFileClient client = account.CreateCloudFileClient();
            CloudFileShare  share  = client.GetShareReference(storage.ShareName);

            await share.CreateIfNotExistsAsync();

            if (!await share.ExistsAsync())
            {
                throw new UploadFailedException($"Share {storage.ShareName} does not exist and could not be created.");
            }

            return(share);
        }
Example #11
0
        public void UploadFileStream(Stream stream, string connectionString, FileInfo outputFile)
        {
            connectionString.ThrowExceptionIfNullOrEmpty();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare fileShare = fileClient.GetShareReference("h2h");

            if (Task.Run(async() => await fileShare.ExistsAsync()).Result)
            {
                string policyName = "DemoPolicy" + new Random().Next(50);

                FileSharePermissions fileSharePermissions = Task.Run(async() => await fileShare.GetPermissionsAsync()).Result;

                // define policy
                SharedAccessFilePolicy sharedAccessFilePolicy = new SharedAccessFilePolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                    //Permissions = SharedAccessFilePermissions.Read
                    Permissions = SharedAccessFilePermissions.Write
                };

                fileSharePermissions.SharedAccessPolicies.Add(policyName, sharedAccessFilePolicy);

                // set permissions of file share
                Task.Run(async() => await fileShare.SetPermissionsAsync(fileSharePermissions));

                // generate SAS token based on policy and use to create a new file
                CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();

                if (Task.Run(async() => await rootDirectory.ExistsAsync()).Result)
                {
                    CloudFileDirectory customDirectory = rootDirectory.GetDirectoryReference("HyperArchive");
                    if (Task.Run(async() => await customDirectory.ExistsAsync()).Result)
                    {
                        CloudFile file     = customDirectory.GetFileReference(outputFile.Name);
                        string    sasToken = file.GetSharedAccessSignature(null, policyName);

                        //generate URL of file with SAS token
                        Uri       fileSASUrl = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);
                        CloudFile newFile    = new CloudFile(fileSASUrl);

                        var taskResult = Task.Run(async() => await newFile.UploadFromStreamAsync(stream));
                    }
                }
            }
        }
Example #12
0
        public void WriteFilesIntoFileService()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_configuration.ReadKeyFromFilePath(Constants.ConnectionStringPathKey));

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare fileShare = fileClient.GetShareReference("h2h");

            if (Task.Run(async() => await fileShare.ExistsAsync()).Result)
            {
                string policyName = "DemoPolicy" + new Random().Next(50);

                FileSharePermissions fileSharePermissions = Task.Run(async() => await fileShare.GetPermissionsAsync()).Result;

                // define policy
                SharedAccessFilePolicy sharedAccessFilePolicy = new SharedAccessFilePolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                    //Permissions = SharedAccessFilePermissions.Read
                    Permissions = SharedAccessFilePermissions.Write
                };

                fileSharePermissions.SharedAccessPolicies.Add(policyName, sharedAccessFilePolicy);

                // set permissions of file share
                Task.Run(async() => await fileShare.SetPermissionsAsync(fileSharePermissions));

                // generate SAS token based on policy and use to create a new file
                CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();

                if (Task.Run(async() => await rootDirectory.ExistsAsync()).Result)
                {
                    CloudFileDirectory customDirectory = rootDirectory.GetDirectoryReference("Output");
                    if (Task.Run(async() => await customDirectory.ExistsAsync()).Result)
                    {
                        CloudFile file     = customDirectory.GetFileReference(_globalNotesPdf.Name);
                        string    sasToken = file.GetSharedAccessSignature(null, policyName);

                        //generate URL of file with SAS token
                        Uri       fileSASUrl = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);
                        CloudFile newFile    = new CloudFile(fileSASUrl);

                        Task.Run(async() => await newFile.UploadFromFileAsync(_globalNotesPdf.FullName));
                    }
                }
            }
        }
        public async Task <MemoryStream> GetFile(string clientId, string filename)
        {
            this.Init();

            CloudFileShare share      = this._cloudFileClient.GetShareReference(this._fileStorageOptions.ShareName);
            bool           shareExist = await share.ExistsAsync();

            if (shareExist != true)
            {
                throw new ServiceOperationException("No such share.");
            }

            //Get root directory
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
            bool rootDirExist = await rootDirectory.ExistsAsync();

            if (rootDirExist != true)
            {
                throw new ServiceOperationException("No such root dir.");
            }

            //Get clients directory
            CloudFileDirectory clientsFolder = rootDirectory.GetDirectoryReference(clientId);
            bool clientsDirExist             = await clientsFolder.ExistsAsync();

            if (clientsDirExist != true)
            {
                throw new ServiceOperationException("No such client dir.");
            }

            //Get reference to file
            //If file already exists it will be overwritten
            CloudFile file       = clientsFolder.GetFileReference(filename);
            bool      fileExists = await file.ExistsAsync();

            if (fileExists != true)
            {
                throw new ServiceOperationException("No such file");
            }

            MemoryStream ms = new MemoryStream();
            await file.DownloadToStreamAsync(ms);

            ms.Position = 0;

            return(ms);
        }
        private async Task <T> AccessToFileAsync <T>(string filePath, Func <CloudFile, Task <T> > delFunction)
        {
            var azureFile = AzureFilePath.FromFilePath(filePath);

            // Get a reference to the file share we created previously.
            CloudFileShare share = fileClient.GetShareReference(azureFile.ShareReference);

            // Ensure that the share exists.
            if (true || await share.ExistsAsync().ConfigureAwait(false)) //Obviamos esta comprobación porque puede que no se tenga privilegios suficientes
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                // Get a reference to the directory we created previously.
                if (azureFile.Folders.Any())
                {
                    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(azureFile.Folders[0]);
                    if (!sampleDir.Exists())
                    {
                        throw new Exception("Incorrect route path.");
                    }
                    for (int i = 1; i < azureFile.Folders.Count; i++)
                    {
                        sampleDir = sampleDir.GetDirectoryReference(azureFile.Folders[i]);
                        if (!sampleDir.Exists())
                        {
                            throw new Exception("Incorrect route path.");
                        }
                    }
                    CloudFile file = sampleDir.GetFileReference(azureFile.FileName);

                    // Ensure that the file exists.
                    return(await delFunction(file).ConfigureAwait(false));
                }
                else
                {
                    CloudFile file = rootDir.GetFileReference(azureFile.FileName);

                    // Ensure that the file exists.
                    return(await delFunction(file).ConfigureAwait(false));
                }
            }
            else
            {
                throw new Exception("Share not found.");
            }
        }
        public async Task <string> GetText()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_configuration.GetConnectionString("StorageAccount"));

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare share = fileClient.GetShareReference(_configuration.GetSection("FileConfig:Folder").Value);

            if (await share.ExistsAsync())
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                CloudFile file = rootDir.GetFileReference(_configuration.GetSection("FileConfig:Filename").Value);

                if (await file.ExistsAsync())
                {
                    var result = file.DownloadTextAsync().Result;
                }
            }

            return(string.Empty);
        }
        public async Task <IEnumerable <Well> > GetWells()
        {
            var key = Resources.AzureStorageKey;
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(key);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare share = fileClient.GetShareReference("welldata");

            if (await share.ExistsAsync())
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                var fileList = await rootDir.ListFilesAndDirectoriesSegmentedAsync(new FileContinuationToken());

                var wellFile   = fileList.Results.Last() as CloudFile;
                var wellString = await wellFile.DownloadTextAsync();

                var wells = JArray.Parse(wellString).ToObject <List <Well> >();
                return(wells);
            }
            return(new List <Well>());
        }
        public ActionResult <string> GetFileContent()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(@"DefaultEndpointsProtocol=https;AccountName=lab1diag679;AccountKey=TtymI1zwujPy40v9NvlAApX1o04SSA4Jnj2TAH4VvlnoOiPbCym4Rt9qLZwVRgeKgPIHskJBcRtcW0Rt9vxFZw==;EndpointSuffix=core.windows.net");
            CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();
            CloudFileShare      share          = fileClient.GetShareReference("lab1-fs");

            if (share.ExistsAsync().Result)
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                // Get a reference to the directory we created previously.
                CloudFile file = rootDir.GetFileReference("Test1.txt");

                if (file.ExistsAsync().Result)
                {
                    // Write the contents of the file to the console window.
                    return(file.DownloadTextAsync().Result);
                }
            }
            return("file not found");
        }
Example #18
0
        /// <summary>
        /// Async Method to get Data from Azure Cloud.
        /// </summary>
        /// <returns>String of the file.</returns>
        private async Task <string> GetData_Async()
        {
            string DataToShare = string.Empty;
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_ConnectionString);
            // Create a new file share, if it does not already exist.
            // Create a CloudFileClient object for credentialed access to File storage.
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  share      = fileClient.GetShareReference(_DirectoryLocation);

            if (await share.ExistsAsync())
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                if (await rootDir.ExistsAsync())
                {
                    CloudFile FiletoUse = rootDir.GetFileReference(_FileName);
                    if (await FiletoUse.ExistsAsync())
                    {
                        DataToShare = await FiletoUse.DownloadTextAsync();
                    }
                }
            }
            return(DataToShare);
        }
Example #19
0
        private async static void DownloadContent()
        {
            CloudFileShare trialFileShare = _cloudFileClient.GetShareReference("trial");

            if (await trialFileShare.ExistsAsync())
            {
                CloudFileDirectory rootDirectory        = trialFileShare.GetRootDirectoryReference();
                CloudFileDirectory textFoldersDirectory = rootDirectory.GetDirectoryReference("text-folders");
                if (await textFoldersDirectory.ExistsAsync())
                {
                    CloudFile myTargetFile = textFoldersDirectory.GetFileReference("my targets.txt");
                    if (await myTargetFile.ExistsAsync())
                    {
                        string content = await myTargetFile.DownloadTextAsync();

                        Console.WriteLine(content);
                    }
                }
            }
            else
            {
                Console.WriteLine("File share doesn't exist");
            }
        }
Example #20
0
        public async Task <IActionResult> GetFile()
        {
            try
            {
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

                IConfigurationRoot configuration = builder.Build();
                var allCitDebtsLines             = new string[10000000];

                //  while (true)
                //{
                //  if (DateTime.Now.AddHours(3).ToShortTimeString() == "09:58:00 aM")
                // {


                //List<string> fileNames = new List<string>();
                //List<string[]> lstCloudFilesdata = new List<string[]>();


                CloudStorageAccount storageAccount = CloudStorageAccount.Parse($"{configuration["ConnectionString1"]}");
                CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();
                CloudFileShare      fileShare      = fileClient.GetShareReference("import");
                //looks for a file share in the cloud
                bool fileShareExists = await fileShare.ExistsAsync();

                if (fileShareExists)
                {
                    List <CloudFile>   lstCloudFiles = new List <CloudFile>();
                    CloudFileDirectory rootDir       = fileShare.GetRootDirectoryReference();

                    List <string> sl = new List <string>();

                    DeptResults       DeptResults = new DeptResults();
                    List <ErrorLines> ErrorLines  = new List <ErrorLines>();
                    ErrorLines        ErrorLine   = new ErrorLines();



                    string bigfilename = "CitizenDebts_1M_3Big.txt";
                    string fileName    = "CitizenDebts_1M_3.txt";
                    //"DEBTS_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    CloudFile file      = rootDir.GetFileReference(fileName);
                    string    checkfile = bigfilename;
                    //if the file exists

                    bool asd = await file.ExistsAsync();

                    if (asd)
                    {
                        //adds new datasting array
                        sl = await ReadDataAsync(file);

                        if (sl is null)
                        {
                            //return NotFound(HttpStatusCode.NotFound + "\n" + "\nΣφάλμα\nΤο αρχείο δεν περιέχει σωστό αριθμό στηλών</b>");
                            Redirect(DeptResults.BillsCount = 0, DeptResults.NewUsers = 0, HttpStatusCode.NotFound.ToString(), null, "\n" + "\nΣφάλμα\nΤο αρχείο δεν περιέχει σωστό αριθμό στηλών</b>");
                        }
                    }
                    else
                    {
                        Redirect(DeptResults.BillsCount = 0, DeptResults.NewUsers = 0, HttpStatusCode.NotFound.ToString(), null, "\n" + "\nΣφάλμα\nΔεν βρέθηκε το αρχείο</b>");

                        // return NotFound(HttpStatusCode.NotFound+ "\n" + "\nΣφάλμα\nΔεν βρέθηκε το αρχείο</b>");
                    }

                    Console.WriteLine("File into List " + DateTime.Now.ToString());
                    //foreach (string y in sl)
                    //{ Console.WriteLine("From list new : " + y); };
                    string[] cols;

                    for (int i = sl.Count - 1; i >= 0; i--)
                    {
                        cols = sl.ElementAt(i).Split(';');
                        if (cols[0].Trim().Length != 10 || !cols[0].All(char.IsDigit))
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος ΑΦΜ";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                            continue;
                            //return NotFound(HttpStatusCode.NotFound + "\n" + "\nΣφάλμα Λάθος ΑΦΜ");}
                        }
                        if (cols[1].Trim().Length == 0)
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Όνομα ";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);     //return NotFound(HttpStatusCode.NotFound + "\n" + "\nΣφάλμα Λάθος Όνομα ");
                        }

                        if (cols[2].Trim().Length == 0)
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Επώνυμο ";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }

                        if (cols[3].Trim().Length == 0 || !Regex.IsMatch(cols[3], @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Email   ";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }

                        if (cols[4].Trim().Length == 0 || !cols[4].All(char.IsDigit))
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Τηλέφωνο  ";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }

                        if (cols[5].Trim().Length == 0)
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Διεύθυσνη ";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }

                        if (cols[6].Trim().Length == 0)
                        {
                            sl.RemoveAt(i); return(NotFound(HttpStatusCode.NotFound + "\n" + "\nΣφάλμα Λάθος Περιοχή "));
                        }
                        //!Regex.IsMatch(cols[7], @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$")
                        if (cols[7].Trim().Length == 0)
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Αρ.Λογαριασμού ";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }

                        if (cols[8].Trim().Length == 0)
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Περιγραφή Λογαριασμού";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }

                        decimal number;
                        if (cols[9].Trim().Length == 0 || !Decimal.TryParse(cols[9], out number) || cols[9].Contains('.'))

                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Ποσό";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }

                        DateTime d;
                        if (cols[10].Trim().Length == 0 || !DateTime.TryParseExact(cols[10], "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out d))
                        {
                            ErrorLine.line         = i;
                            ErrorLine.ErrorMessage = "Σφάλμα Λάθος Ημερομηνία";
                            ErrorLine.LineString   = sl[i];
                            ErrorLines.Add(ErrorLine);
                            sl.RemoveAt(i);
                        }
                    }



                    IEnumerable <CitizenDepts> o = from eachLine in (
                        from inner in sl
                        select inner.Split(';')
                        )

                                                   select new CitizenDepts
                    {
                        VAT              = eachLine[0],
                        FirstName        = eachLine[1],
                        LastName         = eachLine[2],
                        Email            = eachLine[3],
                        Phone            = eachLine[4],
                        Address          = eachLine[5],
                        County           = eachLine[6],
                        BillId           = eachLine[7],
                        Bill_description = eachLine[8],
                        //Amount = Decimal.Parse(eachLine[9]),
                        Amount  = Decimal.Parse(eachLine[9], System.Globalization.CultureInfo.GetCultureInfo("el-GR")),
                        DueDate = DateTime.ParseExact(eachLine[10],
                                                      "yyyyMMdd", CultureInfo.InvariantCulture)
                    };
                    Console.WriteLine("File splitted " + DateTime.Now.ToString());



                    var all = from c in _context.CitizenDepts select c;
                    _context.CitizenDepts.RemoveRange(all);
                    await _context.SaveChangesAsync();

                    Console.WriteLine("CitizenDept table deleted " + DateTime.Now.ToString());

                    foreach (var p in o)
                    {
                        _context.Add(p);
                    }
                    Console.WriteLine("Context filledup " + DateTime.Now.ToString());
                    await _context.SaveChangesAsync();



                    Console.WriteLine("Context saved to DB " + DateTime.Now.ToString());

                    //filter citizens
                    NewCitizens = o.
                                  Where(x => !((_context.ApplicationUser.Any(y => y.VAT == x.VAT)) || (_context.ApplicationUser.Any(y => y.Email == x.Email))))
                                  .Select(x => x).AsEnumerable();
                    Console.WriteLine("Citizens filterd " + DateTime.Now.ToString());


                    foreach (var p in NewCitizens)
                    {
                        Console.WriteLine(p.VAT + " , " + p.Email + " , " + p.LastName + " , " + p.Bill_description);
                    }
                    Console.WriteLine("Citizens log " + DateTime.Now.ToString());
                    //only new citizens

                    foreach (var p in NewCitizens.Distinct())
                    {
                        var user = new ApplicationUser {
                            UserName = p.Email, Email = p.Email
                        };

                        var TempPass = GeneratePassword(3, 3, 3, 3);
                        // int lowercase, int uppercase, int numerics, int symbols
                        Console.WriteLine(TempPass);
                        var result = await _userManager.CreateAsync(user, TempPass);

                        if (result.Succeeded)
                        {
                            if (checkfile != "CitizenDebts_1M_3.txt")
                            {
                                Console.WriteLine("Sending Emails");
                                SendMail(p.LastName, p.Email, TempPass);
                            }


                            var query =
                                from UserUpd in _context.ApplicationUser
                                where UserUpd.Email == p.Email
                                select UserUpd;

                            foreach (ApplicationUser UserUpd in query)
                            {
                                UserUpd.VAT       = p.VAT;
                                UserUpd.IsFirst   = true;
                                UserUpd.LastName  = p.LastName;
                                UserUpd.FirstName = p.FirstName;
                                UserUpd.Phone     = p.Phone;
                                UserUpd.County    = p.County;
                                UserUpd.Address   = p.Address;
                            }
                        }
                        else
                        {
                            Console.WriteLine("#############################ALREADY REGISTERED " + p.VAT + " , " + p.Email + " , " + p.LastName);
                        }
                        NewUsers++;
                    }
                    ;

                    await _context.SaveChangesAsync();

                    Console.WriteLine("New Users Registered and Emailed " + DateTime.Now.ToString());


                    foreach (var a in _context.ApplicationUser)
                    {
                        var query2 =
                            from UIdUpd in _context.CitizenDepts
                            where UIdUpd.VAT == a.VAT
                            select UIdUpd;

                        foreach (CitizenDepts UIdUpd in query2)
                        {
                            UIdUpd.UserGUId = a.Id;
                        }
                    }
                    ;

                    await _context.SaveChangesAsync();



                    List <Bills> NewBillsls = new List <Bills>();
                    Bills        NewBill    = new Bills();

                    List <CitizenDepts> UpdCit = new List <CitizenDepts>();


                    UpdCit.AddRange(_context.CitizenDepts);

                    foreach (var e in UpdCit)
                    {
                        Console.WriteLine("#############################CITIZEN_DEPTS " + e.VAT + " , " + e.Email + " , " + e.UserGUId);
                        NewBill.GuId             = e.BillId;
                        NewBill.Amount           = e.Amount;
                        NewBill.DueDate          = e.DueDate;
                        NewBill.Bill_description = e.Bill_description;
                        NewBill.Status           = 0;
                        NewBill.UserId           = e.UserGUId;
                        NewBill.PaymentMethodId  = 1;
                        NewBill.SettlementId     = 1;
                        NewBillsls.Add(NewBill);
                        NewBill = new Bills();
                    }

                    //....  delete bills
                    var allBills = from c in _context.Bills select c;
                    _context.Bills.RemoveRange(allBills);
                    await _context.SaveChangesAsync();

                    //....  delete settlements
                    _context.Settlements.RemoveRange(
                        _context.Settlements
                        .Where(s => s.ID != 1)
                        );
                    await _context.SaveChangesAsync();

                    _context.Bills.AddRange(NewBillsls);

                    await _context.SaveChangesAsync();

                    string Mes;
                    if (ErrorLines.Count() > 0)
                    {
                        Mes = "Ύπήρχαν " + ErrorLines.Count() + " σφάλματα στην εισαγωγή του αρχείου";
                    }
                    else
                    {
                        Mes = "Δεν Υπήρχαν Σφάλματα στην εισαγωγή του αρχείου";
                    }


                    //  return View(NewCitizens);
                    return(RedirectToAction("DeptResults", "DeptResults", new DeptResults
                    {
                        BillsCount = NewBillsls.Count(),
                        NewUsers = NewUsers,
                        HttpStatus = "",
                        ErrorLines = ErrorLines,
                        Message = Mes
                    }));
                }
                else
                {
                    return(NotFound(HttpStatusCode.NotFound + "\n" + "\nΔεν βρέθηκε ο κοινός φάκελος!"));
                }
            }
            catch (Exception ex)
            {
                return(NotFound(HttpStatusCode.ExpectationFailed + "\n" + ex.Message + "\nΣφάλμα\nΑπροσδιόριστο σφάλμα Στην Εισαγωγή του αρχείου</b>"));
            }
            finally
            {
                Console.WriteLine("Import Finished Successfully " + DateTime.Now.ToString());
            }

            return(View());
        }
        private async Task <IEnumerable <string> > ListFilesOrDirectoriesAsync(string folderPath, bool isDirectory)
        {
            if (!folderPath.StartsWith("/"))
            {
                throw new ArgumentException("First character on folderPath must be / (root).", nameof(folderPath));
            }
            string[] filePathSplitted = folderPath.Split("/");

            if (filePathSplitted.Length < 2)
            {
                throw new ArgumentException("FolderPath correct format is \"/{folder/s}\".", nameof(folderPath));
            }

            // Get a reference to the file share we created previously.
            CloudFileShare share = fileClient.GetShareReference(filePathSplitted[1]);

            // Ensure that the share exists.
            if (true || await share.ExistsAsync().ConfigureAwait(false)) //Obviamos esta comprobación porque puede que no se tenga privilegios suficientes
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory selDir = share.GetRootDirectoryReference();
                if (!selDir.Exists())
                {
                    throw new Exception("Incorrect route path.");
                }
                // Get a reference to the directory we created previously.
                if (filePathSplitted.Length > 2)
                {
                    CloudFileDirectory tempDir = selDir.GetDirectoryReference(filePathSplitted[2]);
                    if (!tempDir.Exists())
                    {
                        throw new Exception("Incorrect route path.");
                    }
                    for (int i = 3; i < filePathSplitted.Length; i++)
                    {
                        tempDir = tempDir.GetDirectoryReference(filePathSplitted[i]);
                        if (!tempDir.Exists())
                        {
                            throw new Exception("Incorrect route path.");
                        }
                    }
                    selDir = tempDir;
                }

                List <IListFileItem>  results = new List <IListFileItem>();
                FileContinuationToken token   = null;
                do
                {
                    FileResultSegment resultSegment = await selDir.ListFilesAndDirectoriesSegmentedAsync(token);

                    results.AddRange(resultSegment.Results);
                    token = resultSegment.ContinuationToken;
                }while (token != null);
                if (isDirectory)
                {
                    return(results.Where(lfi => lfi is CloudFileDirectory).Select(lfi => ((CloudFileDirectory)lfi).Name));
                }
                else
                {
                    return(results.Where(lfi => lfi is CloudFile).Select(lfi => ((CloudFile)lfi).Name));
                }
            }
            else
            {
                throw new Exception("Share not found.");
            }
        }
Example #22
0
        static async System.Threading.Tasks.Task Read_FileAsync()
        {
            try
            {
#if DEBUG || DEV
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile($"appsettings.Development.json", true, true);
#else
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile($"appsettings.json", true, true);
#endif
                //var builder = new ConfigurationBuilder()
                //                    .SetBasePath(Directory.GetCurrentDirectory())
                //                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

                IConfigurationRoot configuration = builder.Build();

                CloudStorageAccount cuentaAlmacenamiento = CloudStorageAccount.Parse(configuration.GetConnectionString("FileConnectionString"));
                CloudFileClient     clienteArchivos      = cuentaAlmacenamiento.CreateCloudFileClient();
                CloudFileShare      archivoCompartido    = clienteArchivos.GetShareReference("platzifile");

                bool existeFileShare = archivoCompartido.ExistsAsync().GetAwaiter().GetResult();
                //bool existeFileShare = await archivoCompartido.ExistsAsync();
                //.Net Framework no funciona el await con el .ExistsAsync()
                //agregar .GetAwaiter().GetResult()

                if (existeFileShare)
                {
                    CloudFileDirectory carpetaRaiz = archivoCompartido.GetRootDirectoryReference();
                    CloudFileDirectory directorio  = carpetaRaiz.GetDirectoryReference("registros");

                    bool existeDirectorio = directorio.ExistsAsync().GetAwaiter().GetResult();

                    if (existeDirectorio)
                    {
                        CloudFile archivo = directorio.GetFileReference(ARCHIVO_NOMBRE);

                        bool existeArchivo = archivo.ExistsAsync().GetAwaiter().GetResult();

                        if (existeArchivo)
                        {
                            Console.WriteLine($"Escribiendo archivo: {ARCHIVO_NOMBRE}");
                            string x = archivo.DownloadTextAsync().GetAwaiter().GetResult();
                            //string x = await archivo.DownloadTextAsync();
                            Console.WriteLine(x);

                            //Console.WriteLine(archivo.DownloadTextAsync().Result);
                        }
                        else
                        {
                            Console.WriteLine("no se encontro el archivo: logActividades.txt");
                        }
                    }
                    else
                    {
                        Console.WriteLine("no se encontro la carpeta: registros");
                    }
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                Console.ReadLine();
            }
        }
Example #23
0
        public async Task <IActionResult> PostFile()
        {
            PostFile model = new PostFile();

            try
            {
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

                IConfigurationRoot configuration = builder.Build();

                var allSettlementsLines = new string[10000000];
                var allPaymentLines     = new string[10000000];

                List <string>       fileNames         = new List <string>();
                List <string[]>     lstCloudFilesdata = new List <string[]>();
                CloudStorageAccount storageAccount    = CloudStorageAccount.Parse($"{configuration["ConnectionString1"]}");
                CloudFileClient     fileClient        = storageAccount.CreateCloudFileClient();
                CloudFileShare      fileShare         = fileClient.GetShareReference("export");
                //looks for a file share in the cloud
                bool fileShareExists = await fileShare.ExistsAsync();



                if (fileShareExists)
                {
                    CloudFileDirectory rootDir = fileShare.GetRootDirectoryReference();

                    var payments = _context.Bills
                                   .Where(s => s.Status == 1).ToList();
                    List <string> PaymentsList = new List <string>();
                    model.PaymentsCount = 0;

                    foreach (var x in payments)
                    {
                        PaymentsList.Add(x.GuId + ";" + x.DueDate.ToUniversalTime().ToString("o") + ";" + x.Amount + ";" + "CREDIT");
                        model.PaymentsCount++;
                    }
                    Console.WriteLine(model.PaymentsCount);
                    string fileName = "PAYMENTS_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    await Export(PaymentsList, rootDir, fileName);

                    var List = _context.ApplicationUser
                               .Join(_context.Bills,
                                     a => a.Id,
                                     b => b.UserId,
                                     (a, b) => new { a, b })
                               .Where(w => w.b.Status == 2)
                               .Join(_context.Settlements,
                                     bb => bb.b.SettlementId,
                                     c => c.ID,
                                     (bb, c) => new { bb, c })
                               .Where(w => w.bb.b.Status == 2)
                               .Select(m => new
                    {
                        VAT          = m.bb.a.VAT,
                        SettlReq     = m.c.RequestDate,
                        Bills        = m.bb.a.Bills,
                        Downpayment  = m.c.DownPayment,
                        Installments = m.c.Installments,
                        Interest     = m.c.Interest,
                        SettlementId = m.c.ID
                    }).ToList();

                    List <string> SettlementsList = new List <string>();
                    model.SettlementsCount = 0;

                    foreach (var x in List.Distinct())
                    {
                        //Console.WriteLine(string.Join(",", x.Bills.Where(w=>w.Status==2).Select(n=>n.GuId)));
                        SettlementsList.Add(x.VAT + ";" + x.SettlReq.ToUniversalTime().ToString("o") + ";" + string.Join(",", x.Bills.Where(w => w.Status == 2 && w.SettlementId == x.SettlementId).Select(n => n.GuId).Distinct()) + ";" + x.Downpayment + ";" + x.Installments + ";" + x.Interest);
                        model.SettlementsCount++;
                    }
                    Console.WriteLine(model.SettlementsCount);
                    fileName = "SETTLEMENTS" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    await Export(SettlementsList, rootDir, fileName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
            }

            return(View(model));
        }
 public static bool Exists(this CloudFileShare share, FileRequestOptions requestOptions = null, OperationContext operationContext = null)
 {
     return(share.ExistsAsync(requestOptions, operationContext).GetAwaiter().GetResult());
 }
Example #25
0
        async Task ExecuteCreateFileShareAsync()
        {
            if (IsBusy)
            {
                return;
            }

            if (!ReadyToSave)
            {
                //This should never happen as the save button should be disabled
                Logger.Report(new Exception("Create file share called when ReadyToSave was false"), "Method", "ExecuteCreateFileShareAsync");
                return;
            }

            if (fileShareName.Length < 3 || fileShareName.Length > 63 ||
                !Regex.IsMatch(fileShareName, @"^[a-z0-9]+(-[a-z0-9]+)*$"))
            {
                MessagingService.Current.SendMessage <MessagingServiceAlert>(MessageKeys.Message, new MessagingServiceAlert
                {
                    Title   = "File Share name is invalid",
                    Message = "File Share names must be between 3 and 63 chars, only contain lowercase letters, numbers, and hyphens, must begin with a number or letter, must not contain consecutive hyphens, or end with a hyphen.",
                    Cancel  = "OK"
                });
                return;
            }


            IProgressDialog savingDialog = UserDialogs.Loading("Saving File Share");

            savingDialog.Show();

            try
            {
                IsBusy = true;
                string connectionString = Constants.StorageConnectionString;
                connectionString = connectionString.Replace("<ACCOUNTNAME>", SelectedStorageAccount.Name);
                connectionString = connectionString.Replace("<ACCOUNTKEY>", SelectedStorageAccount.PrimaryKey);

                CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
                var fileClient         = sa.CreateCloudFileClient();

                CloudFileShare fileShare = fileClient.GetShareReference(FileShareName);
                if (fileShare == null)
                {
                    Console.WriteLine("File Share is null");
                }
                if (await fileShare.ExistsAsync())
                {
                    MessagingService.Current.SendMessage <MessagingServiceAlert>(MessageKeys.Message, new MessagingServiceAlert
                    {
                        Title   = "File Share Exists",
                        Message = "A file share with the name \"" + FileShareName + "\" already exists in this storage account.",
                        Cancel  = "OK"
                    });
                    return;
                }
                else
                {
                    await fileShare.CreateAsync();

                    var realm = App.GetRealm();
                    var storageAccountName = selectedStorageAccount.Name;
                    realm.Write(() =>
                    {
                        realm.Add(new RealmCloudFileShare(fileShare.Name,
                                                          selectedStorageAccount.Name,
                                                          fileShare.Uri.ToString()));
                    });

                    if (fileSharesVM != null)
                    {
                        fileSharesVM.AddFileShare(new ASECloudFileShare(fileShare, selectedStorageAccount.Name));
                        App.Logger.Track(AppEvent.CreatedFileShare.ToString());
                    }
                    //This is here and in finally so we'll dismiss this before popping the page so the
                    //Loader doesn't stay longer than the popup
                    if (savingDialog != null)
                    {
                        if (savingDialog.IsShowing)
                        {
                            savingDialog.Hide();
                        }
                        savingDialog.Dispose();
                    }
                    await PopupNavigation.PopAsync();
                }
            }
            catch (Exception ex)
            {
                Logger.Report(ex, "Method", "ExecuteCreateFileShareAsync");
                MessagingService.Current.SendMessage(MessageKeys.Error, ex);
            }
            finally
            {
                IsBusy = false;
                if (savingDialog != null)
                {
                    if (savingDialog.IsShowing)
                    {
                        savingDialog.Hide();
                    }
                    savingDialog.Dispose();
                }
            }
            return;
        }
        public async Task <FileStreamResult> Get(int id)
        {
            var claimsIdentity = User.Identity as ClaimsIdentity;
            var userId         = Convert.ToInt64(claimsIdentity.Claims.FirstOrDefault(claim => claim.Type == "Id").Value);
            var user           = _multiSourcePlaylistRepository.GetUser(userId);
            var track          = _multiSourcePlaylistRepository.GetTrack(id);

            byte[] audioArray = new byte[0];

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                _configuration["Production:StorageConnectionString"]);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            // Get a reference to the file share we created previously.
            CloudFileShare     share   = fileClient.GetShareReference(user.FileFolder);
            CloudFileDirectory userDir = null;

            // Ensure that the share exists.
            if (await share.ExistsAsync())
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                // Get a reference to the directory we created previously.
                userDir = rootDir.GetDirectoryReference("audio");
                // Ensure that the directory exists.
                if (await userDir.ExistsAsync())
                {
                    var audiofile = userDir.GetFileReference(track.Address);
                    if (await audiofile.ExistsAsync())
                    {
                        await audiofile.FetchAttributesAsync();

                        audioArray = new byte[audiofile.Properties.Length];
                        await audiofile.DownloadToByteArrayAsync(audioArray, 0);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
            long fSize        = audioArray.Length;
            long startbyte    = 0;
            long endbyte      = fSize - 1;
            int  statusCode   = 200;
            var  rangeRequest = Request.Headers["Range"].ToString();

            if (rangeRequest != "")
            {
                string[] range = Request.Headers["Range"].ToString().Split(new char[] { '=', '-' });
                startbyte = Convert.ToInt64(range[1]);
                if (range.Length > 2 && range[2] != "")
                {
                    endbyte = Convert.ToInt64(range[2]);
                }
                if (startbyte != 0 || endbyte != fSize - 1 || range.Length > 2 && range[2] == "")
                {
                    statusCode = 206;
                }
            }

            long desSize = endbyte - startbyte + 1;

            Response.StatusCode  = statusCode;
            Response.ContentType = "audio/mp3";
            Response.Headers.Add("Content-Accept", Response.ContentType);
            Response.Headers.Add("Content-Length", desSize.ToString());
            Response.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", startbyte, endbyte, fSize));
            Response.Headers.Add("Accept-Ranges", "bytes");
            Response.Headers.Remove("Cache-Control");
            var stream = new MemoryStream(audioArray, (int)startbyte, (int)desSize);

            return(new FileStreamResult(stream, "audio/mp3")
            {
                FileDownloadName = track.Name
            });
        }
Example #27
0
        public static async Task <string> UnZip(
            [ActivityTrigger] string name,
            //[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            // Get app settings.
            var destinationStorage            = Environment.GetEnvironmentVariable("DestinationStorageAccount");
            var destinationFileShare          = Environment.GetEnvironmentVariable("DestinationFileShare");
            var sourceStorageConnectionString = Environment.GetEnvironmentVariable("SourceStorageAccount");

            //string name = "test.zip";
            var localZipFile = SetLocalPath(name);

            log.LogInformation($"Blob trigger function Processed blob:{name}");

            // Check whether the connection string can be parsed.
            CloudStorageAccount storageAccount;

            if (CloudStorageAccount.TryParse(sourceStorageConnectionString, out storageAccount))
            {
                // If the connection string is valid, proceed with operations against Blob
                // storage here.
                CloudBlobClient    cloudBlobClient    = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("archived");
                CloudBlockBlob     cloudBlockBlob     = cloudBlobContainer.GetBlockBlobReference(name);
                await cloudBlockBlob.DownloadToFileAsync(localZipFile, FileMode.Create);
            }
            else
            {
                // Otherwise, let the user know that they need to define the environment variable.
                log.LogInformation(
                    "A connection string has not been defined in the system environment variables. " +
                    "Add an environment variable named 'SourceStorageAccount' with your storage " +
                    "connection string as a value.");
            }

            // Parse the connection string for the storage account.
            CloudStorageAccount cloudFileStorageAccount = CloudStorageAccount.Parse(destinationStorage);

            // Create a CloudFileClient object for credentialed access to Azure Files.
            CloudFileClient fileClient = cloudFileStorageAccount.CreateCloudFileClient();

            // Get a reference to the file share.
            CloudFileShare share = fileClient.GetShareReference(destinationFileShare);

            // Get a reference to the root directory for the share.
            CloudFileDirectory destinationDirectory = share.GetRootDirectoryReference();

            // Create file share if it doesn't exist.
            if (!await share.ExistsAsync())
            {
                await share.CreateAsync();
            }

            // Set slash character that is used to differentiate the zip entry from file.
            char slash = '/';

            //var localZipFile = SetLocalPath(name);

            try
            {
                // Filter out only zips.
                if (name.Split('.').Last().ToLower() == "zip")
                {
                    // write the zip to the disk
                    //await myBlob.DownloadToFileAsync(localZipFile, FileMode.Create);

                    // Opening zip file and specifying encoding for entries in the zip.
                    using (ZipArchive archive = ZipFile.Open(localZipFile, 0, Encoding.GetEncoding("ISO-8859-1")))
                    {
                        foreach (ZipArchiveEntry entry in archive.Entries)
                        {
                            /// How to tell if a “ZipArchiveEntry” is directory? - https://stackoverflow.com/questions/40223451/how-to-tell-if-a-ziparchiveentry-is-directory
                            // Check if th zip archive entry is a folder. FullName property for folders end with a "/" and Name property is empty.
                            if (slash == entry.FullName[entry.FullName.Length - 1] && 0 == entry.Name.Length)
                            {
                                // Create a folder if the zip archive entry is a folder.
                                log.LogInformation($"Now processing folder '{entry.FullName}'");
                                CloudFileDirectory EntryDestinationDirectory = destinationDirectory.GetDirectoryReference(entry.FullName);

                                if (!await EntryDestinationDirectory.ExistsAsync())
                                {
                                    await EntryDestinationDirectory.CreateAsync();
                                }
                            }

                            // Check if the zip archive entry is a file.
                            if (slash != entry.FullName.Length - 1 && 0 != entry.Name.Length)
                            {
                                log.LogInformation($"Now processing file '{entry.FullName}'");

                                //// Create buffer that is used to measure the deflated file size
                                byte[] buf  = new byte[1024];
                                int    size = 0;

                                // Open the entry to measure the size
                                using (var fileStream = entry.Open())
                                {
                                    int len;
                                    while ((len = fileStream.Read(buf, 0, buf.Length)) > 0)
                                    {
                                        size += len;
                                    }
                                }

                                //var threadCount = 1;

                                //if (size > 83886080)
                                //{
                                //    threadCount = 4;
                                //}
                                //else
                                //{
                                //    threadCount = 1;

                                //}

                                var requestOptions = new FileRequestOptions()
                                {
                                    ParallelOperationThreadCount = 8
                                };

                                // Open the zip entry for further processing
                                using (var fileStream = entry.Open())
                                {
                                    // Open memory stream by specifying the size
                                    using (var fileMemoryStream = new MemoryStream(size + 1024))
                                    //using (var fileMemoryStream = new MemoryStream())
                                    {
                                        fileStream.CopyTo(fileMemoryStream);
                                        fileMemoryStream.Position = 0;
                                        var destinationFile = destinationDirectory.GetFileReference(entry.FullName);
                                        await destinationFile.UploadFromStreamAsync(fileMemoryStream, null, requestOptions, null);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogInformation($"Error! Something went wrong: {ex.Message}, {ex.StackTrace}");
            }

            //log.LogInformation($"cleaning up temp files {localZipFile}");
            //CleanUp(localZipFile);
            log.LogInformation($"Unzip of '{name}' completed!");
            return("OK!");
        }
Example #28
0
        private static async Task mainAsync(string[] args)
        {
            try
            {
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

                IConfigurationRoot configuration = builder.Build();
                var allCitDebtsLines             = new string[10000000];
                var allSettlementsLines          = new string[10000000];
                var allPaymentLines = new string[10000000];
                while (true)
                {
                    if (DateTime.Now.AddHours(3).ToShortTimeString() == "12:00 PM")
                    {
                        List <string>       fileNames         = new List <string>();
                        List <string[]>     lstCloudFilesdata = new List <string[]>();
                        CloudStorageAccount storageAccount    = CloudStorageAccount.Parse($"{configuration["ConnectionString1"]}");
                        CloudFileClient     fileClient        = storageAccount.CreateCloudFileClient();
                        CloudFileShare      fileShare         = fileClient.GetShareReference("import");
                        //looks for a file share in the cloud
                        bool fileShareExists = await fileShare.ExistsAsync();

                        if (fileShareExists)
                        {
                            List <CloudFile>   lstCloudFiles = new List <CloudFile>();
                            CloudFileDirectory rootDir       = fileShare.GetRootDirectoryReference();
                            //for each file in my fileshare
                            FileContinuationToken token = null;
                            FileResultSegment     k     = await rootDir.ListFilesAndDirectoriesSegmentedAsync(token);

                            token = k.ContinuationToken;

                            //var context_ = new Db.Data.();
                            List <string> sl = new List <string>();

                            foreach (IListFileItem fiile in k.Results)
                            {
                                //if the file exists
                                CloudFile file = (CloudFile)fiile;
                                bool      asd  = await file.ExistsAsync();

                                if (asd)
                                {
                                    //adds new datasting array
                                    sl = await ReadDataAsync(lstCloudFilesdata, file, fileNames);
                                }

                                foreach (string y in sl)
                                {
                                    Console.WriteLine("From list new : " + y);
                                }
                                ;

                                IEnumerable <CitizenDepts> o = from eachLine in (
                                    from inner in sl
                                    select inner.Split(';')
                                    )
                                                               select new CitizenDepts
                                {
                                    VAT              = eachLine[0],
                                    FirstName        = eachLine[1],
                                    LastName         = eachLine[2],
                                    Email            = eachLine[3],
                                    Phone            = eachLine[4],
                                    Address          = eachLine[5],
                                    County           = eachLine[6],
                                    BillId           = eachLine[7],
                                    Bill_description = eachLine[8],
                                    Amount           = Decimal.Parse(eachLine[9]),
                                    DueDate          = DateTime.ParseExact(eachLine[10],
                                                                           "yyyyMMdd", CultureInfo.InvariantCulture)
                                };
                                foreach (var p in o)
                                {
                                    Console.WriteLine(p.FirstName + " - " + p.BillId + ", - " + p.DueDate);
                                }
                                ;



                                //string s = context_.Database.ProviderName;

                                // Console.WriteLine(s);
                                /// var all = from c in context_.CitizenDepts select c;
                                //context_.CitizenDepts.RemoveRange(all);
                                /// context_.SaveChanges();

                                foreach (var p in o)
                                {
                                    //Add Student object into Students DBset
                                    //if (p.VAT!=null)
                                    //context_.Add(p);
                                }
                                //// call SaveChanges method to save student into database
                                //context_.SaveChanges();
                            }
                        }
                        if (lstCloudFilesdata != null && fileNames != null)
                        {
                            ProccessData(lstCloudFilesdata, fileNames);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
            }
        }
Example #29
0
        public async Task RunPermissionsTestFiles(SharedAccessAccountPolicy policy)
        {
            CloudFileClient fileClient = GenerateCloudFileClient();
            string          shareName  = "s" + Guid.NewGuid().ToString("N");

            try
            {
                CloudStorageAccount account           = new CloudStorageAccount(fileClient.Credentials, false);
                string              accountSASToken   = account.GetSharedAccessSignature(policy);
                StorageCredentials  accountSAS        = new StorageCredentials(accountSASToken);
                CloudStorageAccount accountWithSAS    = new CloudStorageAccount(accountSAS, null, null, null, fileClient.StorageUri);
                CloudFileClient     fileClientWithSAS = accountWithSAS.CreateCloudFileClient();
                CloudFileShare      shareWithSAS      = fileClientWithSAS.GetShareReference(shareName);
                CloudFileShare      share             = fileClient.GetShareReference(shareName);

                // General pattern - If current perms support doing a thing with SAS, do the thing with SAS and validate with shared
                // Otherwise, make sure SAS fails and then do the thing with shared key.

                // Things to do:
                // Create the share (Create / Write perms, Container RT)
                // List shares with prefix (List perms, Service RT)
                // Create a new file (Create / Write, Object RT)
                // Add a range to the file (Write, Object RT)
                // Read the data from the file (Read, Object RT)
                // Overwrite a file (Write, Object RT)
                // Delete the file (Delete perms, Object RT)

                if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Container) == SharedAccessAccountResourceTypes.Container))
                {
                    await shareWithSAS.CreateAsync();
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await shareWithSAS.CreateAsync(), "Creating a share with SAS should fail without Create or Write and Container-level perms.");

                    await share.CreateAsync();
                }
                Assert.IsTrue(await share.ExistsAsync());

                if (((policy.Permissions & SharedAccessAccountPermissions.List) == SharedAccessAccountPermissions.List) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Service) == SharedAccessAccountResourceTypes.Service))
                {
                    Assert.AreEqual(shareName, (await fileClientWithSAS.ListSharesSegmentedAsync(shareName, null)).Results.First().Name);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => (await fileClientWithSAS.ListSharesSegmentedAsync(shareName, null)).Results.First(), "Listing shared with SAS should fail without List and Service-level perms.");
                }

                string    filename    = "fileName";
                CloudFile fileWithSAS = shareWithSAS.GetRootDirectoryReference().GetFileReference(filename);
                CloudFile file        = share.GetRootDirectoryReference().GetFileReference(filename);

                //Try creating credentials using SAS Uri directly
                CloudFile fileWithSASUri = new CloudFile(new Uri(share.Uri + accountSASToken));

                byte[] content = new byte[] { 0x1, 0x2, 0x3, 0x4 };
                if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await fileWithSAS.CreateAsync(content.Length);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.CreateAsync(content.Length), "Creating a file with SAS should fail without Create or Write and Object-level perms.");

                    await file.CreateAsync(content.Length);
                }
                Assert.IsTrue(await file.ExistsAsync());

                using (MemoryStream stream = new MemoryStream(content))
                {
                    if (((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write) &&
                        ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                    {
                        await fileWithSAS.WriteRangeAsync(stream, 0, null);
                    }
                    else
                    {
                        await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.WriteRangeAsync(stream, 0, null), "Writing a range to a file with SAS should fail without Write and Object-level perms.");

                        stream.Seek(0, SeekOrigin.Begin);
                        await file.WriteRangeAsync(stream, 0, null);
                    }
                }

                byte[] result = new byte[content.Length];
                await file.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length);

                for (int i = 0; i < content.Length; i++)
                {
                    Assert.AreEqual(content[i], result[i]);
                }

                if (((policy.Permissions & SharedAccessAccountPermissions.Read) == SharedAccessAccountPermissions.Read) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    result = new byte[content.Length];
                    await fileWithSAS.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length);

                    for (int i = 0; i < content.Length; i++)
                    {
                        Assert.AreEqual(content[i], result[i]);
                    }
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length), "Reading a file with SAS should fail without Read and Object-level perms.");
                }

                if (((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await fileWithSAS.CreateAsync(2);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.CreateAsync(2), "Overwriting a file with SAS should fail without Write and Object-level perms.");

                    await file.CreateAsync(2);
                }

                result = new byte[content.Length];
                await file.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length);

                for (int i = 0; i < content.Length; i++)
                {
                    Assert.AreEqual(0, result[i]);
                }

                if (((policy.Permissions & SharedAccessAccountPermissions.Delete) == SharedAccessAccountPermissions.Delete) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await fileWithSAS.DeleteAsync();
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.DeleteAsync(), "Deleting a file with SAS should fail without Delete and Object-level perms.");

                    await file.DeleteAsync();
                }

                Assert.IsFalse(await file.ExistsAsync());
            }
            finally
            {
                fileClient.GetShareReference(shareName).DeleteIfExistsAsync().Wait();
            }
        }