~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/webservices/.svn/text-base/Download.ashx.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
|
3
 
| Copyright (c) 2007 Novell, Inc.
4
 
| All Rights Reserved.
5
 
|
6
 
| This program is free software; you can redistribute it and/or
7
 
| modify it under the terms of version 2 of the GNU General Public License as
8
 
| published by the Free Software Foundation.
9
 
|
10
 
| This program is distributed in the hope that it will be useful,
11
 
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
| GNU General Public License for more details.
14
 
|
15
 
| You should have received a copy of the GNU General Public License
16
 
| along with this program; if not, contact Novell, Inc.
17
 
|
18
 
| To contact Novell about this file by physical or electronic mail,
19
 
| you may find current contact information at www.novell.com 
20
 
|
21
 
| Author: Rob
22
 
|***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.IO;
26
 
using System.Web;
27
 
using System.Net;
28
 
 
29
 
using Simias.Client;
30
 
using Simias.Storage;
31
 
 
32
 
namespace iFolder.WebService
33
 
{
34
 
        /// <summary>
35
 
        /// File Download Handler
36
 
        /// </summary>
37
 
        public class DownloadHandler : FileHandler
38
 
        {
39
 
                const int BUFFERSIZE = (64 * 1024);
40
 
 
41
 
                /// <summary>
42
 
                /// Constructor
43
 
                /// </summary>
44
 
                public DownloadHandler() : base()
45
 
                {
46
 
                }
47
 
 
48
 
                #region IHttpHandler Members
49
 
 
50
 
                /// <summary>
51
 
                /// Process the Request
52
 
                /// </summary>
53
 
                /// <param name="context">The HttpContext object.</param>
54
 
                public override void ProcessRequest(HttpContext context)
55
 
                {
56
 
                        try
57
 
                        {
58
 
                                // initialize
59
 
                                Initialize(context);
60
 
 
61
 
                                // does node exist
62
 
                                if (node == null)
63
 
                                {
64
 
                                        string id = ((entryID != null) && (entryID.Length != 0)) ? entryID : entryPath;
65
 
                                        throw new EntryDoesNotExistException(id);
66
 
                                }
67
 
 
68
 
                                // lock the file
69
 
                                FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
70
 
                        
71
 
                                try
72
 
                                {
73
 
                                        // response
74
 
                                        context.Response.Clear();
75
 
                                        context.Response.AddHeader("Content-Disposition",
76
 
                                                String.Format("attachment; filename=\"{0}\"",
77
 
                                                HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8).Replace("+", "%20")));
78
 
                                        context.Response.AddHeader("Content-Length", stream.Length.ToString());
79
 
                                        context.Response.ContentType = "application/octet-stream";
80
 
                                        context.Response.BufferOutput = false;
81
 
                                        
82
 
                                        Stream output = context.Response.OutputStream;
83
 
 
84
 
                                        byte[] buffer = new byte[BUFFERSIZE];
85
 
                                        int count = 0;
86
 
 
87
 
                                        while((count = stream.Read(buffer, 0, BUFFERSIZE)) > 0)
88
 
                                        {
89
 
                                                output.Write(buffer, 0, count);
90
 
                                                output.Flush();
91
 
                                        }
92
 
 
93
 
                                        // log
94
 
                                        log.LogAccess("Download", node.GetRelativePath(), node.ID, "Success");
95
 
                                }
96
 
                                catch
97
 
                                {
98
 
                                        // log
99
 
                                        log.LogAccess("Download", node.GetRelativePath(), node.ID, "Failed");
100
 
 
101
 
                                        throw;
102
 
                                }
103
 
                                finally
104
 
                                {
105
 
                                        // release the file
106
 
                                        stream.Close();
107
 
                                }
108
 
                        }
109
 
                        catch(Exception e)
110
 
                        {
111
 
                                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
112
 
                                context.Response.StatusDescription = e.GetType().Name;
113
 
                        }
114
 
                }
115
 
 
116
 
                #endregion
117
 
        }
118
 
}