Exemple #1
0
        public override bool Process(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session)
        {
            if (request.UriPath.StartsWith(Url))
            {
                string board       = request.QueryString[UrlParameters.Board].Value;
                string threadIdStr = request.QueryString[UrlParameters.ThreadId].Value;
                int    threadId    = -1;
                int.TryParse(threadIdStr, out threadId);

                if (!Program.IsBoardLetterValid(board))
                {
                    ThreadServerModule.write_text("Invalid board letter", response);
                    return(true);
                }

                if (threadId <= 0)
                {
                    ThreadServerModule.write_text("Invalid thread id", response);
                    return(true);
                }

                PostFormatter[] thread_data = ThreadStore.GetStorageEngine().GetThread(board, threadIdStr);

                MemoryStream memIO = new MemoryStream();

                ZipOutputStream zipStream = new ZipOutputStream(memIO);
                zipStream.SetLevel(0); // no compression is needed since most of the files are media files, and they are already compressed anyway

                write_file_to_zip(zipStream, "res/blue.css", Encoding.UTF8.GetBytes(Properties.Resources.css_blue));
                write_file_to_zip(zipStream, "res/sticky.png", Properties.Resources.sticky);
                write_file_to_zip(zipStream, "res/locked.png", Properties.Resources.locked);

                foreach (PostFormatter pf in thread_data)
                {
                    if (pf.MyFile != null)
                    {
                        string full_path;
                        if (FileOperations.ResolveFullFilePath(pf.MyFile.Hash, pf.MyFile.Extension, out full_path))
                        {
                            string ext = Path.GetExtension(full_path);

                            if (!string.IsNullOrEmpty(ext))
                            {
                                ext = ext.Substring(1);
                            }

                            if (ext != pf.MyFile.Extension)
                            {
                                pf.MyFile.ChangeExtension(ext);
                            }

                            string zip_file_name = string.Format("file/{0}.{1}", pf.MyFile.Hash, ext);

                            byte[] data = File.ReadAllBytes(full_path);

                            pf.MyFile.Size = data.Length;

                            write_file_to_zip(zipStream, zip_file_name, data);
                        }

                        string thumb_path;

                        if (FileOperations.CheckThumbFileExist(pf.MyFile.Hash, out thumb_path))
                        {
                            string zip_file_name = string.Format("thumb/{0}.jpg", pf.MyFile.Hash);
                            write_file_to_zip(zipStream, zip_file_name, File.ReadAllBytes(thumb_path));
                        }
                    }
                }

                string notes = ThreadStore.GetStorageEngine().GetThreadNotes(board, threadId);

                string pageHtml = build_page_html(board, threadIdStr, thread_data, notes);

                write_file_to_zip(zipStream, "index.html", Encoding.UTF8.GetBytes(pageHtml));

                zipStream.Close();
                memIO.Close();

                byte[] result = memIO.ToArray();

                response.Status        = System.Net.HttpStatusCode.OK;
                response.ContentType   = ServerConstants.ZipContentType;
                response.ContentLength = result.Length;
                response.AddHeader("content-disposition", string.Format("attachment; filename=\"{0}.zip\"", threadId));
                response.SendHeaders();
                response.SendBody(result);

                return(true);
            }
            return(false);
        }