Imports System.Web
Imports System.Web.Services
Imports Nuvolar.Module
Imports DevExpress.ExpressApp.FileAttachments
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Data.Filtering
Imports System.IO
Imports DevExpress.Web.Internal

Public Class GetAttachments
    Implements System.Web.IHttpHandler
    Private _Session As DevExpress.Xpo.Session
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim _Oid As String
        Dim _ConnectionString As String
        Dim _Attachment As FileData
        Dim _MemoryStream64 As String
        _Oid = context.Request.QueryString("id")
        _MemoryStream64 = context.Request.QueryString("ms")
        If _Oid <> "" Then
            _ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString

            _Session = New DevExpress.Xpo.Session
            _Session.ConnectionString = _ConnectionString
            _Session.AutoCreateOption = AutoCreateOption.SchemaAlreadyExists
            _Session.Connect()

            If _Session.IsConnected Then
                _Attachment = _Session.FindObject(Of FileData)(CriteriaOperator.Parse("Oid=?", _Oid))

                If _Attachment IsNot Nothing Then
                    Dim _Stream As New MemoryStream
                    _Attachment.SaveToStream(_Stream)
                    HttpUtils.WriteFileToResponse(Nothing, _Stream, _Attachment.FileName, True, "")
                End If
            End If
        End If
        If _MemoryStream64 = "1" Then
            If GLOBAL_Download_MemoryStream IsNot Nothing Then
                GLOBAL_Download_MemoryStream.Position = 0
                HttpUtils.WriteFileToResponse(Nothing, GLOBAL_Download_MemoryStream, GLOBAL_Download_FileName, True, GLOBAL_Download_FileExtension)
            End If
        End If

    End Sub
    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class