Example #1
0
        /// <summary>
        /// Создаёт объект с реплицируемым файлов по версии файла.
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        public static WcfRemoteFile FromFileVersion(IFileVersion version)
        {
            if (version == null)
            {
                throw new ArgumentNullException("version");
            }

            return(new WcfRemoteFile
            {
                Name = version.Name,
                Stream = version.Open(),
                TimeCreated = version.TimeCreated,
                UniqueID = version.File.UniqueID,
                VersionID = version.UniqueID
            });
        }
Example #2
0
        public async Task Process(HttpListenerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            this.Logger.WriteMessage("Process:Начало отдачи содержимого по сессионной ссылке");

            Stream sourceStream  = null;
            string fileName      = null;
            bool   isInline      = false;
            long   contentLength = 0;

            string url = context.Request.Url.AbsoluteUri;

            this.Logger.WriteMessage(String.Format("Process:url: {0}", url));

            try
            {
                this.Authorize(context);

                HttpParamsReader parametersReader = new HttpParamsReader(url);
                isInline = parametersReader.GetBooleanParameterValue("inline");

                using (IFileVersion version = this.Engine.ResolveSessionLink(url, context.User.Identity))
                {
                    this.Logger.WriteMessage(String.Format("Process:url: {0}, version.Name: {1}, version.UniqueID: {2}", url, version.Name, version.UniqueID));

                    contentLength = version.Size;
                    fileName      = version.Name;
                    sourceStream  = version.Open();
                }
            }
            catch (Exception ex)
            {
                string responseString = string.Format("Ошибка при обработке ссылки {0}. Текст ошибки: {1}",
                                                      url,
                                                      ex);
                fileName = "error.txt";

                byte[] content = Encoding.UTF8.GetBytes(responseString);
                contentLength = content.Length;
                sourceStream  = new MemoryStream(content);
                context.Response.ContentEncoding = Encoding.UTF8;

                this.Logger.WriteMessage(String.Format("Process:{0}", responseString), LogLevel.Error);
            }
            finally
            {
                try
                {
                    string title = string.Format(string.Format("filename*=UTF-8''{0}", Uri.EscapeDataString(fileName)));

                    context.Response.ContentLength64 = contentLength;
                    context.Response.ContentType     = "application/octet-stream";
                    string fileDisposition = isInline ? "inline" : "attachment";
                    context.Response.Headers.Add("Content-Disposition", string.Format("{0}; {1}", fileDisposition, title));
                    context.Response.StatusCode = (int)HttpStatusCode.OK;

                    using (sourceStream)
                    {
                        byte[] buffer = new byte[1024 * 1024];
                        int    read   = 0;
                        while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            context.Response.OutputStream.Write(buffer, 0, read);
                        }
                    }
                    this.Logger.WriteMessage(
                        String.Format("Process:Окончание отдачи содержимого по сессионной ссылке, url: {0}", url));
                }
                catch (Exception innerEx)
                {
                    this.Logger.WriteMessage(
                        String.Format("Process:Ошибка отдачи содержимого клиенту, текст ошибки: {0}", innerEx),
                        LogLevel.Error);
                }
            }
        }