Get size of Site directory in Orchard - directory

crosspost: https://orchard.codeplex.com/discussions/456226
In Orchard, each site (whether or not you enable multitenancy) seems to have it's own Folder within Media (main file folder for Orchard). I want to get the entire filesize of a current site (ergo, the folder under Media).
I've digged into the Framework and got into FileSystemStorageProvider which seems to be promising with the FileSystemStorageFolder class and GetSize() method.
However, I was wondering if anyone else checked this out before I go into experimenting with that class.
Any piece of information or advise would be highly apreciated. Thanks!

Didn't really find an easy way to do it but copied mostly from the Orchard framework. You will need the following:
private FileSystemStorageProvider _filesystemProvider;
private ShellSettings _settings;
And then you need to define the Site Storage Path:
var mediaPath = HostingEnvironment.IsHosted
? HostingEnvironment.MapPath("~/Media/") ?? ""
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Media");
storagePath = Path.Combine(mediaPath, _settings.Name);
Finally, here is my function to compute the storage for a specific folder (in this case, the tenant's/site's root media folder):
public double GetSiteStorage()
{
var folders = _filesystemProvider.ListFolders(storagePath);
long totalSize = 0;
foreach (var folder in folders)
{
totalSize += folder.GetSize();
}
return (totalSize / 1024 / 1024);
}
This returns a double for the MB used. Hope this helps someone :)

Related

Read (audio) file from subfolder in AndroidAssets in Xamarin.Forms

I try to eneable audio in my Xamarin.Forms application. I want to have the audio files in a Subfolderof the Assetsfolder like this Assets/Subfolder/Audio.mp3
I have found a plugin SimpleAudioPlayer which provide an example.
The following code works like provided.
var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
player.Load("Audio.mp3");
Now I want to place the audio file in a subfolder and I tried to call
player.Load("Subfolder/Audio.mp3");
But I get an Java.IO.FileNotFoundException
I looked then into the implementation of the Load function and I fould the following code
public bool Load(string fileName)
{
player.Reset();
AssetFileDescriptor afd = Android.App.Application.Context.Assets.OpenFd(fileName);
player?.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
return PreparePlayer();
}
where the filename is pasted to the Assets.OpenFd() function. which returns an AndroidFileDesriptor
The documentation does not really provide any information from Microsoft and from the Android site.
My questions are
How can I receive the file from the subfolder in Android Assets?
What can I paste into the Assets.OpenFd() function (subfolders etc)?
I would appreciate any advice, since after a long time trying to resolve it I don't really have an idea.
According to the docs of the package, you won't be able to do just that because you have to use player.Load(GetStreamFromFile("mysound.wav")); where GetStreamFromFile is basically
Stream GetStreamFromFile(string filename)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("App." + filename);
// Obviously App in the line above should be replaced with your app
return stream;
}
And secondly you have to player.Load(GetStreamFromFile("Subfolder.mysound.wav")); where Subfolder is the name of your subfolder.

How to get virtual directory physical path

As we know that a virtual direcoty can be linked to a folder with a diffrent name, how can I get the physical path of a virtual directory ?
I've been trying with HttpContext.Current.server.MapPath but it returns me the physic path plus the path I send in parameter even if the directory doesn't even exist or if it exists with a diffrent name.
Exemple :
C:\blabla\Sites\Application1\Imaageesss
- On disc
Application1\Images (In ISS, my virutal directory)
But if I do a MapPath on "/Images" it will never give me
C:\blabla\Sites\Application1\Imaageesss but
C:\inetpub\wwwroot\Images which is not the real directory linked to.
Server.MapPath("~/Images")
is the correct way to go about it as "~" references the root of your application.
This is what worked for me:
string physicalPath =
System.Web.Hosting.HostingEnvironment.MapPath(HttpContext.Current.Request.ApplicationPath);
What if you try this little snippet?
string physicalPath = HttpContext.Current.Request.MapPath(appPath);
After some more research I was able to create a method to get the physical path of a virtual IIS directory:
public static string VirtualToPhysicalPath(string vPath) {
// Remove query string:
vPath = Regex.Replace(vPath, #"\?.+", "").ToLower();
// Check if file is in standard folder:
var pPath = System.Web.Hosting.HostingEnvironment.MapPath("~" + vPath);
if (System.IO.File.Exists(pPath)) return pPath;
// Else check for IIS virtual directory:
var siteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
var sm = new Microsoft.Web.Administration.ServerManager();
var vDirs = sm.Sites[siteName].Applications[0].VirtualDirectories;
foreach (var vd in vDirs) {
if (vd.Path != "/" && vPath.Contains(vd.Path.ToLower())) pPath = vPath.Replace(vd.Path.ToLower(), vd.PhysicalPath).Replace("/", "\\");
}
return pPath;
}
Caveat: this solution assumes that you only have a root application (Applications[0]).
The following should work just fine:
var physicalPath = HostingEnvironment.MapPath("~/MyVirtualDirectory");
This might answer your question:
http://msdn.microsoft.com/en-us/library/system.web.httprequest.physicalpath.aspx
However, I can't currently provide an example, because I have got a lot of work to do. When I'll find some time I'll send detailed information.

Blackberry - Cannot create SQLite database

I am making an app that runs in the background, and starts on device boot.
I have read the docs, and have the SQLiteDemo files from RIM, and I am using them to try create a database on my SD Card in the simulator.
Unfortunately, I am getting this error:
DatabasePathException:Invalid path name. Path does not contains a proper root list. See FileSystemRegistry class for details.
Here's my code:
public static Database storeDB;
public static final String DATABASE_NAME = "testDB";
private String DATABASE_LOCATION = "file:///SDCard/Databases/MyDBFolder/";
public static URI dbURI;
dbURI = URI.create(DATABASE_LOCATION+DATABASE_NAME);
storeDB = DatabaseFactory.openOrCreate(dbURI);
I took out a try/catch for URI.create and DatabaseFactory.openOrCreate for the purposes of this post.
So, can anyone tell me why I can't create a database on my simulator?
If I load it up and go into media, I can create a folder manually. The SD card is pointing to a folder on my hard drive, and if I create a folder in there, it is shown on the simulator too, so I can create folders, just not programatically.
Also, I have tried this from the developer docs:
// Determine if an SDCard is present
boolean sdCardPresent = false;
String root = null;
Enumeration enum = FileSystemRegistry.listRoots();
while (enum.hasMoreElements())
{
root = (String)enum.nextElement();
System.err.println("root="+root);
if(root.equalsIgnoreCase("sdcard/"))
{
sdCardPresent = true;
}
}
But it only picks up store/ and never sdcard/.
Can anyone help?
Thanks.
FYI,
I think I resolved this.
The problem was I was trying to write to storage during boot-up, but the storage wasn't ready. Once the device/simulator was loaded, and a few of my listeners were triggered, the DB was created.
See here:
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/832062/How_To_-_Write_safe_initialization_code.html?nodeid=1487426&vernum=0

FLEX: getting a folder size

I'm triying to get a folder size by doing:
var FolderFile:File = new File("file:///SomePath/Folder");
var FolderSize: FolderFile.size;
But this gives me a value of 0, how can I get the folder size? is there anyway to do this?
Tranks
No, there's no way to do it automagically. Getting the size of the directory is a complex and potentially painfully slow operation. There could be 10s of thousands of files in a directory, or a directory could be located on a (slow?) network, not to mention tape storage and similar scenarios.
The file systems themselves don't store directory size information, and the only way to know it is to calculate it file-by-file, there's no quick/easy shortcut. So, you will have to rely on the solution you posted above, and, yes, it is going to be slow.
I want to know the size of the folder (like 10mb). Sorry for the second line, I write it wrong, it's:
var Foldersize:Number = FolderFile.size;
I just made a new class wich executes this function:
public function GetFolderSize(Source:Array):Number
{
var TotalSizeInteger:Number = new Number();
for(var i:int = 0;i<Source.length;i++){
if(Source[i].isDirectory){
TotalSizeInteger += this.GetFoldersize(Source[i].getDirectoryListing());
}
else{
TotalSizeInteger += Source[i].size;
}
}
return TotalSizeInteger;
}
In "Source" you pass the FolderFile.getDirectoryListing(), something like this:
var CC:CustomClass = new CustomClass();
var FolderSize:Number = CustomClass.GetFolderSize(FolderFile.getDirectoryListing());
But this is a very slow method, is there a more quick and easy way to know the folder size?
Sorry for my grammar, i'm just learning english.
Thanks

Count images in some folder on WEB server (ASP.NET)

I need to count and check how much of some images is placed in folder od web server.
Example- images get names from user_id, and on example I have user_id 27, and my images are:
27_1.jpg, 27_2.jpg, 27_3.jpg, ...
How to check and write to database this thing?
Thanks
Once you know your path you can use IO.Directory.GetFiles() method.
IO.Directory.GetFiles("\translated\path","27_*.jpg").Count()
will give you what you're looking for.
Using the System.IO namespace, you can do something like this
public File[] GetUserFiles(int userId)
{
List<File> files = new List<File>();
DirectoryInfo di = new DirectoryInfo(#"c:\folderyoulookingfor");
foreach(File f in di.GetFiles())
{
if(f.ToString().StartsWith(userId.ToString()))
files.Add(f);
}
return file.ToArray();
}

Resources