Storing data in local folder is limited - c#

I am trying to save data locally to my device app folder.
When I try to save collected data on an actual Android smartphone, it doesn't work. It is limited by name and filetype, as I cannot change it from test.txt and it is limited in string length, as a maximum of twelve characters get saved.
I have the acquired the following permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This is my code:
MainPage.xaml.cs
private async void ExportData(object sender, EventArgs e)
{
var items = await App.Database.GetDataAsync();
DependencyService.Get<IFileService>().CreateFile(items);
}
Interface
using System;
using System.Collections.Generic;
using System.Text;
namespace LocationApp.Interface
{
public interface IFileService
{
void CreateFile(List<LocationData> items);
}
}
Service
using Android.App;
using LocationApp.Droid;
using LocationApp.Interface;
using System.Collections.Generic;
using System.IO;
[assembly:Xamarin.Forms.Dependency(typeof(FileService))]
namespace LocationApp.Droid
{
public class FileService : IFileService
{
public string GetRootPath()
{
return Application.Context.GetExternalFilesDir(null).ToString();
}
public void CreateFile(List<LocationData> items)
{
var fileName = "test-file.txt";
var destination = Path.Combine(GetRootPath(), fileName);
string[] text = new string[items.Count];
for (int i = 0; i < text.Length; i++)
{
text[i] = $"{items[i].Latitude},{items[i].Longitude},{items[i].Day},{items[i].Time}";
}
File.WriteAllLines(destination, text);
}
}
}
I also attempted to see what would happen to an emulator, I used a Pixel 2 with Android 9.0, API 28 where I got the following error:
[ContextImpl] Failed to ensure /storage/120E-0B1B/Android/data/com.companyname.locationapp/files: java.lang.IllegalStateException: Failed to prepare /storage/120E-0B1B/Android/data/com.companyname.locationapp/files/: android.os.ServiceSpecificException: (code -13)
In the end, I only care about putting all my data in a single file. The filename or the error on my emulator I provided in case the error is based on that. If not, I do not care if they are fixed/fixable.

Based on your code, I created a simple demo, and it works on my android emulator(android 11) .
You can test on your side.
The code is:
public void CreateFile(List<LocationData> items)
{
var fileName = "test-file.txt";
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var destination = Path.Combine(documentsPath, fileName);
string[] text = new string[items.Count];
for (int i = 0; i < text.Length; i++)
{
text[i] = $"{items[i].Latitude},{items[i].Longitude}";
}
File.WriteAllLines(destination, text);
}
And after I saved the data,I could get the saved data by the following code(the filename is test-file.txt):
public string ReadData(string filename)
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var filePath = Path.Combine(documentsPath, filename);
return File.ReadAllText(filePath);
}

Related

How to I extract only the lowest-level objects in Xbim?

I have a BIM model in IFC format and I want to add a new property, say cost, to every object in the model using Xbim. I am building a .NET application. The following code works well, except, the property is also added to storeys, buildings and sites - and I only want to add it to the lowest-level objects that nest no other objects.
To begin with, I have tried various methods to print the "related objects" of each object, thinking that I could filter out any objects with non-null related objects. This has led me to look at this:
IfcRelDefinesByType.RelatedObjects (http://docs.xbim.net/XbimDocs/html/7fb93e55-dcf7-f6da-0e08-f8b5a70accf2.htm) from thinking that RelatedObjects (https://standards.buildingsmart.org/IFC/RELEASE/IFC2x3/FINAL/HTML/ifckernel/lexical/ifcreldecomposes.htm) would contain this information.
But I have not managed to implement working code from this documentation.
Here is my code:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Xbim.Ifc;
using Xbim.Ifc2x3.Interfaces;
using Xbim.Ifc4.Kernel;
using Xbim.Ifc4.MeasureResource;
using Xbim.Ifc4.PropertyResource;
using Xbim.Ifc4.Interfaces;
using IIfcProject = Xbim.Ifc4.Interfaces.IIfcProject;
namespace MyPlugin0._1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
outputBox.AppendText("Plugin launched successfully");
}
private void button1_Click(object sender, EventArgs e)
{
// Setup the editor
var editor = new XbimEditorCredentials
{
ApplicationDevelopersName = "O",
ApplicationFullName = "MyPlugin",
ApplicationIdentifier = "99990100",
ApplicationVersion = "0.1",
EditorsFamilyName = "B",
EditorsGivenName = "O",
EditorsOrganisationName = "MyWorkplace"
};
// Choose an IFC file to work with
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
string filename = dialog.FileName;
string newLine = Environment.NewLine;
// Check if the file is valid and continue
if (!filename.ToLower().EndsWith(".ifc"))
{
// Output error if the file is the wrong format
outputBox.AppendText(newLine + "Error: select an .ifc-file");
}
else
{
// Open the selected file (## Not sure what the response is to a corrupt/invalid .ifc-file)
using (var model = IfcStore.Open(filename, editor, 1.0))
{
// Output success when the file has been opened
string reversedName = Form1.ReversedString(filename);
int filenameShortLength = reversedName.IndexOf("\\");
string filenameShort = filename.Substring(filename.Length - filenameShortLength, filenameShortLength);
outputBox.AppendText(newLine + filenameShort + " opened successfully for editing");
////////////////////////////////////////////////////////////////////
// Get all the objects in the model ( ### lowest level only??? ###)
var objs = model.Instances.OfType<IfcObjectDefinition>();
////////////////////////////////////////////////////////////////////
// Create and store a new property
using (var txn = model.BeginTransaction("Store Costs"))
{
// Iterate over all the walls to initiate the Point Source property
foreach (var obj in objs)
{
// Create new property set to host properties
var pSetRel = model.Instances.New<IfcRelDefinesByProperties>(r =>
{
r.GlobalId = Guid.NewGuid();
r.RelatingPropertyDefinition = model.Instances.New<IfcPropertySet>(pSet =>
{
pSet.Name = "Economy";
pSet.HasProperties.Add(model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "Cost";
p.NominalValue = new IfcMonetaryMeasure(200.00); // Default Currency set on IfcProject
}));
});
});
// Add property to the object
pSetRel.RelatedObjects.Add(obj);
// Rename the object
outputBox.AppendText(newLine + "Cost property added to " + obj.Name);
obj.Name += "_withCost";
//outputBox.AppendText(newLine + obj.OwnerHistory.ToString());
}
// Commit changes to this model
txn.Commit();
};
// Save the changed model with a new name. Does not overwrite existing files but generates a unique name
string newFilename = filenameShort.Substring(0, filenameShort.Length - 4) + "_Modified.IFC";
int i = 1;
while (File.Exists(newFilename))
{
newFilename = filenameShort.Substring(0, filenameShort.Length - 4) + "_Modified(" + i.ToString() + ").IFC";
i += 1;
}
model.SaveAs(newFilename); // (!) Gets stored in the project folder > bin > Debug
outputBox.AppendText(newLine + newFilename + " has been saved");
};
}
}
// Reverse string-function
static string ReversedString(string text)
{
if (text == null) return null;
char[] array = text.ToCharArray();
Array.Reverse(array);
return new String(array);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
You're starting out by getting too broad a set of elements in the model. Pretty much everything in an IFC model will be classed as (or 'derived from') an instance of IfcObjectDefinition - including Spatial concepts (spaces, levels, zones etc) as well as more abstract concepts of Actors (people), Resources and the like.
You'd be better off filtering down objs to the more specific types such as IfcElement, IfcBuildingElement - or even the more real world elements below (IfcWindow, IfcDoor etc.)
// Get all the building elements in the model
var objs = model.Instances.OfType<IfcBuildingElement>();
You could also filter by more specific clauses more than just their type by using the other IFC relationships.

Illegal character in path when running Azure function

I have an Azure function that has been working all day. About twenty minutes ago I started getting an error when trying to click the run button in Visual Studio. It successfully builds and then when it starts it displays and error the just states an 'illegal character in path - Visual Studio'. I've checked the git logs and no changes have been made so I'm unsure what's wrong.
I've attached a picture of the problem to try illustrate the issue:
I have tried everything I can think of. Here's a list of all the steps I've taken.
Restart machine
Re-cloned the repo
Re-installed Visual Studio 2017 & 2019
Deleted the appdata folder
Cleared Cache files
Browsed the debug menus
System restored
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Zupa.Products.ProductsService.Models.Messages.V1;
namespace Zupa.ProductFileUploadFunction
{
public static class FileUploadProcessor
{
private const string FunctionName = nameof(FileUploadProcessor);
[FunctionName(FunctionName)]
public static void Run(
[BlobTrigger("%IncomingContainerPath%/{blobFileName}", Connection = "AzureWebJobsStorage")]Stream inputBlob,
[ServiceBus("%QueueName%", Connection = "QueueServiceBus")]out string outgoingMessage,
string blobFileName, ILogger log)
{
log.LogInformation($"{FunctionName} Processing blob {Environment.NewLine} Name:{blobFileName + Environment.NewLine} Size: {inputBlob.Length} Bytes");
outgoingMessage = null;
var (parsedOrganisationId, parsedUploadId) = (Guid.Empty, Guid.Empty);
var organisationId = GetPartFromString(blobFileName, "/", 0);
var fileUploadType = GetPartFromString(blobFileName, "/", 1);
var isRecognisedFileUploadType = Enum.GetNames(typeof(FileUploadType)).Select(name => name.ToLowerInvariant()).Contains(fileUploadType.ToLowerInvariant());
var uploadId = GetPartFromString(blobFileName, "/", 2);
var fileName = GetPartFromString(blobFileName, "/", 3);
var propertyValidation = new Dictionary<string, bool>()
{
{
nameof(FileUploadDataEventMessage.OrganisationId),
string.IsNullOrEmpty(organisationId) || !Guid.TryParse(organisationId, out parsedOrganisationId)
},
{
nameof(FileUploadDataEventMessage.FileUploadType),
string.IsNullOrEmpty(fileUploadType) || isRecognisedFileUploadType
},
{
nameof(FileUploadDataEventMessage.UploadId),
string.IsNullOrEmpty(uploadId) || !Guid.TryParse(uploadId, out parsedUploadId)
},
{
nameof(FileUploadDataEventMessage.FileName),
string.IsNullOrEmpty(fileName)
}
};
foreach (KeyValuePair<string, bool> propertyValidationPair in propertyValidation)
LogPropertyInErrorState(log, propertyValidationPair.Value, blobFileName, propertyValidationPair.Key);
var hasInvalidPathParameters = parsedOrganisationId == Guid.Empty || !isRecognisedFileUploadType || parsedUploadId == Guid.Empty || string.IsNullOrEmpty(fileName);
outgoingMessage = !hasInvalidPathParameters ? JsonConvert.SerializeObject(new FileUploadDataEventMessage()
{
OrganisationId = parsedOrganisationId,
UploadId = parsedUploadId,
FileUploadType = Enum.Parse<FileUploadType>(PascalCaseWord(fileUploadType)),
FileName = fileName,
Timestamp = DateTimeOffset.Now
}) : null;
log.LogInformation($"{FunctionName} Processed blob {Environment.NewLine} Name:{blobFileName + Environment.NewLine} Size: {inputBlob.Length} Bytes");
}
private static string GetPartFromString(string inputString, string delimiter, int targetIndex)
{
var inputStringParts = inputString.Split(delimiter);
if (inputStringParts.ElementAtOrDefault(targetIndex) != null)
return inputStringParts[targetIndex];
return null;
}
private static void LogPropertyInErrorState(ILogger log, bool errorValidationCondition, string fileName, string propertyName)
{
var errorMessage = errorValidationCondition ?
$"{FunctionName} Failed to process blob {Environment.NewLine} Name: {fileName + Environment.NewLine} Missing or invalid {propertyName}" : string.Empty;
if (!string.IsNullOrEmpty(errorMessage))
log.LogInformation(errorMessage);
}
private static string PascalCaseWord(string input) =>
input.Substring(0, 1).ToUpperInvariant() + input.Substring(1);
}
}
It doesn't seem to be the issue with the function as the rest of the team doesn't seem to experience the same issue and can successfully run the function. So I'm assuming its a system issue.
For anyone experiencing this issue I have finally found a way to fix it
Thank you so much Tsuyoshi Ushio over on medium, follow the steps below to resolve the issue seems to be when the azure function tools files get in a bad state within the %appdata%/local folder
https://medium.com/#tsuyoshiushio/visual-studio-2017-2019-fails-when-i-create-an-azure-functions-project-89e993ef31f
You need to delete the following folder:
%localappdata%\AzureFunctionsTools

using the same chrome profile (session) for different ChromeDriver instances

I am trying to open multiple browsers in parallel, but I can not navigate to the website in more than one window..
Here is how I do it:
namespace XXX
{
public class CoreDriver
{
public IWebDriver driver;
public int my_port { get; set; }
public void Initialize()
{
string chromeee = "";
if (my_port == 50147) { chromeee = "C:/Users/AA/Downloads/chromedriver1/"; }
else if (my_port == 50148) {chromeee = "C:/Users/AA/Downloads/chromedriver2/"; }
else if (my_port == 50149) { chromeee = "C:/Users/AA/Downloads/chromedriver3/"; }
else if (my_port == 50140) { chromeee = "C:/Users/AA/Downloads/chromedriver4/"; }
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\AA\\AppData\\Local\\Google\\Chrome\\User Data");
var driverService = ChromeDriverService.CreateDefaultService(chromeee);
driverService.HideCommandPromptWindow = true;
driverService.Port = my_port;
driver = new ChromeDriver(driverService, options);
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0,0,12));
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(13));
//driver navigate
}
}
}
calling it as this:
CoreDriver A1 = new CoreDriver();
A1.my_port = 50147;
A1.Initialize();
CoreDriver A2 = new CoreDriver();
A2.my_port = 50148;
A2.Initialize(); // timeout error here
// ...
Unfortunately, after the second window is opened - timeout error is shownn:
A first chance exception of type 'OpenQA.Selenium.WebDriverException'
occurred in WebDriver.dll
Additional information: The HTTP request to the remote WebDriver
server for URL http:/loca1host:50148/session timed out after 60
seconds.
at this line:
driver = new ChromeDriver(driverService, options);
after rerunning the test with different parameters I have found out that the error is shown due to the specified Chrome profile:
options.AddArgument("user-data-dir=C:\\Users\\AA\\AppData\\Local\\Google\\Chrome\\User
Data");
If I remove the line - then all of my cookies will not be used in ChromeDriver instance and that is not something that I can live with :)
Is there a way to use the same chrome profile in multiple chromedriver instances?
Okay, so I am using my approach as stated above.
My requirements were:
I must keep the cookies of the main chrome profile
I must keep extensions of the main profile
I do not need the history, opened tabs, session etc. of the main profile
after a new start of an existing custom profile - i start it clear without opened tabs
Here is the logic in few words.
First I specify a directory for the existing Google Chrome profile.
If we need to create cookies (i.e. login into some website) then we do it on the main profile of google chrome.
After it is done, close the chrome. Some websites keep cookies for a long time, some - not. So it is in our interest to relogin on the main profile when necessary. Do not keep the Original chrome opened! Otherwise ChromeDriver will throw some warnings.
Next, my script will copy the necessary folders and files into new folder. This folder is our new profile with all cookies. Everything is about 30 megabytes in size on my PC.
If the folder for the new profile already exists - then the program will only copy cookies files. That's shouldn't be more than 1-2 megs of data.
And here is the code. You might want to tweak one thing or another.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Remote;
using System.IO;
using System.Drawing.Imaging;
using System.Management;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System.Net;
namespace NAMESPACE
{
public class CoreDriver
{
public IWebDriver driver;
public string my_name { get; set; }
public int my_port { get; set; }
public string default_profile_dir = #"C:\Users\USERNAME\AppData\Local\Google\Chrome\";
public string chromedriver_path = #"C:\Users\USERNAME\Downloads\chromedriver_win32\";
public string site_profile_path;
public string site_profile_path_s;
public string default_path;
public void Initialize()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("--log-level=3");
options.AddArgument("--test-type");
options.AddArgument("--silent");
options.AddArgument("user-data-dir=" + site_profile_path_s);
options.AddArgument("--disable-plugins"); // disable flash
var driverService = ChromeDriverService.CreateDefaultService(chromedriver_path);
driverService.HideCommandPromptWindow = true;
driverService.Port = my_port;
driver = new ChromeDriver(driverService, options);
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 14));
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(15));
IJavaScriptExecutor jscript = driver as IJavaScriptExecutor;
jscript.ExecuteScript("return window.stop");
}
public void ConfigureProfile()
{
site_profile_path_s = default_profile_dir + "profile " + my_name;
site_profile_path = site_profile_path_s + #"\Default";
default_path = default_profile_dir + #"User Data\Default";
if (!Directory.Exists(site_profile_path))
{
CreateBlankProfile();
}
else
{
// copy existing chrome profile. Keep cache, extensions, etc.
CopyProfileFiles();
// but stay away from opened tabs
RemoveOpenedTabsFiles();
}
}
public void CleanUpOldProfiles()
{
DirectoryInfo di = new DirectoryInfo(default_profile_dir);
DirectoryInfo[] directories = di.GetDirectories("profile*", SearchOption.TopDirectoryOnly);
if (directories.Count() > 0)
{
foreach (var folder in directories)
{
try
{
Directory.Delete(folder.FullName, true);
}
catch
{
}
}
}
}
public void CreateBlankProfile()
{
// new profile direftory
CreateIfMissing();
// copy existing chrome profile. Keep cache, extensions, etc.
// but stay away from opened tabs
CopyProfileFiles();
CopyProfileFolders();
}
public void CopyProfileFiles()
{
// default profile location
DirectoryInfo di = new DirectoryInfo(default_path);
// copy files
List<string> file_lib = new List<string>() { "Cookies", "Login", "Preferences", "Secur" };
FileInfo[] files = di.GetFiles("*", SearchOption.TopDirectoryOnly);
if (files.Count() > 0)
{
foreach (var file in files)
{
if (PassFileOrFolder(file.Name, file_lib))
{
file.CopyTo(site_profile_path + #"\" + file.Name, true);
}
}
}
}
public void RemoveOpenedTabsFiles()
{
// default profile location
DirectoryInfo di = new DirectoryInfo(site_profile_path);
// copy files
List<string> file_lib = new List<string>() { "Current", "Last" };
FileInfo[] files = di.GetFiles("*", SearchOption.TopDirectoryOnly);
if (files.Count() > 0)
{
foreach (var file in files)
{
if (PassFileOrFolder(file.Name, file_lib))
{
File.Delete(file.FullName);
}
}
}
}
public void CopyProfileFolders()
{
// default profile location
DirectoryInfo di = new DirectoryInfo(default_path);
// copy folders
List<string> folder_lib = new List<string>() { "databases", "Extension", " Storage", "Web Applications", "File System", "IndexedDB" };
DirectoryInfo[] directories = di.GetDirectories("*", SearchOption.TopDirectoryOnly);
if (directories.Count() > 0)
{
foreach (var folder in directories)
{
if (PassFileOrFolder(folder.Name, folder_lib))
{
DirectoryCopy(folder.FullName, site_profile_path + #"\" + folder.Name, true);
}
}
}
}
private void CreateIfMissing()
{
Directory.CreateDirectory(site_profile_path);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
public bool PassFileOrFolder(string input, List<string> library)
{
foreach (string name in library)
{
if (input.Contains(name))
{
return true;
}
}
return false;
}
}
}
Please note that I have also implemented a method to clean up all profiles CleanUpOldProfiles
Review the code, make changes to directories etc. After done - make a following call:
CoreDriver something = new CoreDriver(); // creating an object
// settings
something.my_port = 50150; // multiple chrome instances - will be run on different ports
// I am currently having 4 chrome profiles ;)
something.my_name = "mynewprofile"; // full profile name will be: 'profile + my_name'. Check the code of the object.
// void
something.ConfigureProfile(); // creating new profile or updating existing one, if folder eists
something.Initialize(); // starting the browser
sorry for a long answer. Hope it helps you guys somehow :)

FileSystemProvider for Umbraco v6.1

I'm trying to create a new MediaFileSystemProvider which seamlessly stores Media in Azure Blob Storage.
I copied the MediaFileSystem class from Umbraco v6.1 source for my starting point.
I then edited the /config/FileSystemProviders.config file inserting my new class details.
When I restart Umbraco, the new class is called but I get the error:
"Could not find constructor for type 'mysite.core.umbracoExtensions.FileSystemProviders.AzureBlobStorageProvider, mysite.core' which accepts 0 parameters"
Here's my class:
...
[FileSystemProvider("media")]
public class AzureBlobStorageProvider : FileSystemWrapper
{
private string rootUrl;
CloudStorageAccount storageAccount;
CloudBlobClient blobClient;
CloudBlobContainer container;
public AzureBlobStorageProvider(IFileSystem wrapped)
: base(wrapped)
{
var constring = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;
// Retrieve storage account from connection string.
storageAccount = CloudStorageAccount.Parse(constring);
// Create the blob client.
blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
container = blobClient.GetContainerReference("mymedia");
//container.CreateIfNotExists();
//container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
rootUrl = "https://mysite.blob.core.windows.net/media";
}
...
methods
Any idea what I'm doing wrong here?
Cheers
I couldn't leave this half done considering all the help I've received from folks here :)
I setup a fresh Umbraco v6.1.6 and confirmed the MediaService.Deleted event definitely does not get fired/hooked for me with the below code. I'll go find out how to submit a bug...
For anyone else interested in storing Umbraco Media Items in Azure Storage, below is how I did it. You can turn the CDN on/off for displaying image content with the "CDNEnabled" key and turn uploading on/off with the "AzureCDNUploadEnabled" key without having to touch your Views each time.
FYI, the actual CDN part of Azure Blob Storage is not available at the moment. It was and now isn't and apparently will be again some day.
You can limit your data usage and speed up image delivery by setting the "AzureCDNCacheControlHeader" value to update the Cache-Control header as you upload. The values below set the images to expire in 30 days and then revalidate.
Add this to your web.config appsettings node:
<!-- cdn config -->
<!-- used for razor rendering -->
<add key="CDNPath" value="https://utest.blob.core.windows.net"/>
<add key="CDNEnabled" value="true"/>
<!-- used for media uploads -->
<add key="AzureCDNStorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName={yourAccount};AccountKey={yourKey}"/>
<add key="AzureCDNStorageAccountName" value="{yourStorageAccount}"/>
<add key="AzureCDNBlobContainerName" value="media"/>
<add key="AzureCDNRootUrl" value="https://{yourAccount}.blob.core.windows.net"/>
<add key="AzureCDNUploadEnabled" value="true"/>
<add key="AzureCDNCacheControlHeader" value="must-revalidate, public, max-age=604800"/> <!-- change to whatever suits you -->
<!-- end cdn -->
This is the EventHandler:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Configuration;
using System.Web;
using System.Linq;
using System.IO;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace utest1.umbracoExtensions.events
{
public class SaveMediaToAzure : ApplicationEventHandler
{
/* either add your own logging class or remove this and all calls to 'log' */
private log4net.ILog log = log4net.LogManager.GetLogger(typeof(utest1.logging.PublicLogger));
CloudStorageAccount storageAccount;
private string blobContainerName;
CloudBlobClient blobClient;
CloudBlobContainer container;
string cacheControlHeader;
private bool uploadEnabled;
public SaveMediaToAzure()
{
try
{
storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureCDNStorageConnectionString"]);
blobContainerName = ConfigurationManager.AppSettings["AzureCDNStorageAccountName"];
blobClient = storageAccount.CreateCloudBlobClient();
container = blobClient.GetContainerReference(ConfigurationManager.AppSettings["AzureCDNBlobContainerName"]);
uploadEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings["AzureCDNUploadEnabled"]);
cacheControlHeader = ConfigurationManager.AppSettings["AzureCDNCacheControlHeader"];
MediaService.Saved += MediaServiceSaved;
MediaService.Trashed += MediaServiceTrashed;
MediaService.Deleted += MediaServiceDeleted; // not firing
}
catch (Exception x)
{
log.Error("SaveMediaToAzure Config Error", x);
}
}
void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
{
if (uploadEnabled)
{
foreach (var fileItem in e.SavedEntities)
{
try
{
log.Info("Saving media to Azure:" + e.SavedEntities.First().Name);
var path = fileItem.GetValue("umbracoFile").ToString();
var filePath = HttpContext.Current.Server.MapPath(path);
UploadToAzure(filePath, path);
if (fileItem.GetType() == typeof(Umbraco.Core.Models.Media))
{
UploadThumbToAzure(filePath, path);
}
}
catch (Exception x)
{
log.Error("Error saving media to Azure: " + fileItem.Name, x);
}
}
}
}
/*
* Using this because MediaServiceDeleted event is not firing in v6.1.6
*
*/
void MediaServiceTrashed(IMediaService sender, MoveEventArgs<IMedia> e)
{
if (uploadEnabled)
{
try
{
log.Info("Deleting media from Azure:" + e.Entity.Name);
var path = e.Entity.GetValue("umbracoFile").ToString();
CloudBlockBlob imageBlob = container.GetBlockBlobReference(StripContainerNameFromPath(path));
imageBlob.Delete();
CloudBlockBlob thumbBlob = container.GetBlockBlobReference(StripContainerNameFromPath(GetThumbPath(path)));
thumbBlob.Delete();
}
catch (Exception x)
{
log.Error("Error deleting media from Azure: " + e.Entity.Name, x);
}
}
}
/*
* MediaServiceDeleted event not firing in v6.1.6
*
*/
void MediaServiceDeleted(IMediaService sender, DeleteEventArgs<IMedia> e)
{
//if (uploadEnabled)
//{
// try
// {
// log.Info("Deleting media from Azure:" + e.DeletedEntities.First().Name);
// var path = e.DeletedEntities.First().GetValue("umbracoFile").ToString();
// CloudBlockBlob imageBlob = container.GetBlockBlobReference(StripContainerNameFromPath(path));
// imageBlob.Delete();
// CloudBlockBlob thumbBlob = container.GetBlockBlobReference(StripContainerNameFromPath(GetThumbPath(path)));
// thumbBlob.Delete();
// }
// catch (Exception x)
// {
// log.Error("Error deleting media from Azure: " + e.DeletedEntities.First().Name, x);
// }
//}
Console.WriteLine(e.DeletedEntities.First().Name); // still not working
}
private string StripContainerNameFromPath(string path)
{
return path.Replace("/media/", String.Empty);
}
/*
*
*
*/
private void UploadToAzure(string filePath, string relativePath)
{
System.IO.MemoryStream data = new System.IO.MemoryStream();
System.IO.Stream str = System.IO.File.OpenRead(filePath);
str.CopyTo(data);
data.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
Stream stream = data as Stream;
if (stream.CanSeek)
{
stream.Position = 0;
CloudBlockBlob blob = container.GetBlockBlobReference(StripContainerNameFromPath(relativePath));
blob.UploadFromStream(stream);
SetCacheControl(blob);
}
else
{
log.Error("Could not read image for upload: " + relativePath);
}
}
private void SetCacheControl(CloudBlockBlob blob)
{
blob.Properties.CacheControl = cacheControlHeader;
blob.SetProperties();
}
private void UploadThumbToAzure(string filePath, string relativePath)
{
var thumbFilePath = GetThumbPath(filePath);
var thumbRelativePath = GetThumbPath(relativePath);
UploadToAzure(thumbFilePath, thumbRelativePath);
}
private string GetThumbPath(string path)
{
var parts = path.Split('.');
var filename = parts[parts.Length - 2];
return path.Replace(filename, filename + "_thumb");
}
}
}
This is the RenderHelper:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace utest1.umbracoExtensions.helpers
{
public class CDNImage
{
public static string ConvertUrlToCDN(string source)
{
if (String.IsNullOrEmpty(source))
{
return null;
}
var cdnUrl = System.Configuration.ConfigurationManager.AppSettings["CDNPath"];
var cdnOn = System.Configuration.ConfigurationManager.AppSettings["CDNEnabled"];
if (cdnOn == "true")
{
/*
* check if the url is absolute or not and whether it should be intercepted - eg. an external image url
* if it's absolute you'll need to strip out everything before /media...
*/
if (source.Contains(GetBaseUrl()))
{
source = StripBaseUrl(source);
}
}
return source;
}
private static string GetBaseUrl()
{
var url = System.Web.HttpContext.Current.Request.Url;
var baseUrl = url.Scheme + "//" + url.Host;
if (url.Port != 80 && url.Port != 443)
{
baseUrl += ":" + url.Port;
}
return baseUrl;
}
private static string StripBaseUrl(string path)
{
return path.Replace(GetBaseUrl(), String.Empty);
}
}
}
and finally displaying in the RazorView:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = "BasePage.cshtml";
}
#using utest1.umbracoExtensions.helpers
#{
var ms = ApplicationContext.Current.Services.MediaService;
var img = ms.GetById(int.Parse(CurrentPage.Image));
}
<h1>Umbraco on Azure is getting there!</h1>
<p>#img.Name</p>
<img alt="#img.Name" src="#CDNImage.ConvertUrlToCDN(img.GetValue("umbracoFile").ToString())" />
Suggestions for improvement are welcome.
Aaaah, it feels good to give back :)
Why is it that I spend hours trying to find the answer and then find it soon after I post?
The issue was two fold:
1) I should've been implementing IFileSystem (took inspiration from AmazonS3Provider source)
2) the parameter name being passed from FileSystemProviders.config file was not included in the constructor

DownloadFileAsync multiple files using webclient

Description
Download multiple files using webclient's DownloadFileAsync and utilizing a text file for URL input for download.
Problem
The approach that I have used won't download files at all. Just runs and does nothing. It fills the list array then quits the program without downloading a single file. I have googled for solutions but come up shorthanded. Then attempted to search for a solution in the database here with same results. Any help is appreciated.
Questions
Why does this approach not work?
What can I do to improve this and learn from this.
Code
DownloadClass.cs
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace ThreadTest
{
class DownloadClass
{
public struct download
{
public static string URL { get; set; }
public static string file { get; set; }
public static string[] link;
public static int downloadcount;
}
public static List<string> list = new List<string>();
public static WebClient wc = new WebClient();
public static void Download()
{
int count = 0;
download.URL = list[0];
Uri URI = new Uri(download.URL);
UriBuilder uri = new UriBuilder(URI);
download.link = uri.Path.ToLower().Split(new char[] { '/' });
count = 0;
// Find file
foreach (string abs in download.link)
{
count++;
if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt"))
{
try
{
download.file = download.link[count];
wc.Proxy = null;
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(URI, Application.StartupPath + "\\" + download.file);
break;
}
catch (Exception)
{ }
}
}
}
public static void BeginDownload()
{
new Thread(Download).Start();
}
public static void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
int count = 0;
download.downloadcount++;
download.URL = list[0];
Uri URI = new Uri(download.URL);
UriBuilder uri = new UriBuilder(URI);
download.link = uri.Path.ToLower().Split(new char[] { '/' });
count = 0;
// Find file
foreach (string abs in download.link)
{
count++;
if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt"))
{
try
{
download.file = download.link[count];
}
catch (Exception)
{ }
}
}
list.RemoveAt(0);
if (list.Count > 0)
{
wc.DownloadFileAsync(URI, list[download.downloadcount], Application.StartupPath + "\\" + download.file);
}
else
{
Console.WriteLine("Downloading is done.");
Environment.Exit(0);
}
}
}
}
Program.cs (Main Class)
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ThreadTest
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: {0} <download txtfile>", Environment.GetCommandLineArgs()[0]);
Environment.Exit(0);
}
int counter = 0;
string line;
string format = string.Format("{0}\\{1}", Application.StartupPath, args[0]);
// Read the file line by line.
using(StreamReader file = new StreamReader(format))
{
while ((line = file.ReadLine())!= null)
{
// Store urls in a list.
DownloadClass.list.Add(line);
counter++;
}
}
DownloadClass.BeginDownload();
}
}
}
Besides being bad design there are lots of issues that lead to your code not (or nor correctly working).
You need to make sure that you application lives while it downloads something. Your current app quits right away (you have to wait for the downloading to complete in your main).
You application may download the same file multiple times but not download others at all (You need to completely lock object when they are used in an async=multithreading way like here when accessing static objects) BTW: Don't use static objects at all to avoid that in the first place.
Even if 2 is corrected it may still download the same file multiple times into the same filename and thus fail.
As long as you have no knowledge about multithreading I'd recommend you use the synchoneous methods to avoid all those problems.

Categories

Resources