Wednesday, May 6, 2009

Reporting content downloads: geeks area

Hi everyone,
here's a little contribution to the world from yours truly. This time, highly geeky (beware!).

I had a problem where I needed to report content downloads by mobile phones. Most mobile browsers I know do not allow download and redirect, or vice versa, which would have been nice, as I'd report on the download into the database before or after the actual download happens.

Then I turned to the generic reporting service that my hosting provider has. They have two services they use for reporting. One's a fancy service (smartstats), but in my host environment, their API wasn't installed.
The second option was to go to the raw log files. Easy peasy. Well, not that easy:
- The log files live in a hidden system folder
- The logs are zipped, and host (which is shared) doesn't have an unzipping solution installed in their GAC. You need a 3rd party (ideally free) unzipping utility (I'm using .Net) that can live in the bin folder.

Locating the unzip utility was easy, and I found a great one (kudos!) here. I didn't deal with the documentation and went right for the 'reduced' dll version since I figured if I'm only unzipping a file, that would be sufficient. looks like it worked.

The next piece was to grab the zip files somehow. That was a pain and a waste of time. I tried to go for FTP, but counldn't figure it out. Eventually I found this 3-line piece which did the trick.

Here's a snippet of the relevant pieces of the code for your convenience (below). It's missing one important thing: clean up the files once you're done.

Enjoy! (Hope this will save you some time if you're having similar challenges)
public static processZipFile()
        string zipFileName = "ex"+ t.AddDays(-1).ToString("yyMMdd"),
        // temporary folder the zip file has been saved at
tempPath = AppDomain.CurrentDomain.BaseDirectory + "templog\\";
        // get the zip file from the hidden folder on the server    
// open the file for unzipping
            Ionic.Zip.ZipFile zip = ZipFile.Read(tempPath + zipFileName + ".zip");
            foreach (ZipEntry e in zip)
// unzip the file
                e.Extract(tempPath, true);
// do what you need to do with the file

// get the zip file from the remote server and copy it to a temporary folder

    public static void copyOver(string fileName)
        WebClient wc = new WebClient();
        wc.Credentials = new NetworkCredential("username", "password");
        wc.DownloadFile(""+fileName+".zip", AppDomain.CurrentDomain.BaseDirectory + "templog/"+fileName+".zip");

No comments: