Beispiel #1
0
        private async Task <UploadFileStream> handleRequest(RequestParameters requestParameters,
                                                            HttpResponseSender responseSender)
        {
            UploadFileStream ret = null;

            if (requestParameters.endPoint == "/upload.json")
            {
                if (requestParameters.method != "POST")
                {
                    throw new Http400Exception();
                }
                ret = new UploadFileStream(requestParameters.boundary);
            }
            else if (requestParameters.method == "GET" &&
                     requestParameters.endPoint.StartsWith("/downloads/"))
            {
                await responseSender.serveMediaFile(requestParameters.endPoint);
            }
            else if (requestParameters.method == "GET")
            {
                await responseSender.serveStaticFile(requestParameters.endPoint);
            }
            else
            {
                throw new Http404Exception();
            }

            return(ret);
        }
Beispiel #2
0
        private async void ProcessRequestAsync(StreamSocket socket)
        {
            HttpResponseSender responseSender = new HttpResponseSender(socket);

            try
            {
                using (IInputStream input = socket.InputStream)
                {
                    byte[]  data   = new byte[BufferSize];
                    IBuffer buffer = data.AsBuffer();

                    MemoryStream     headerStream     = new MemoryStream();
                    UploadFileStream uploadFileStream = null;

                    RequestParameters requestParameters = null;
                    bool  headerParsed          = false;
                    ulong payloadLengthReceived = 0;
                    while (true)
                    {
                        buffer = await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);

                        if (buffer.Length == 0)
                        {
                            break; // Connection closed.
                        }
                        if (headerParsed)
                        {
                            if (uploadFileStream != null)
                            {
                                await uploadFileStream.WriteAsync(buffer);
                            }
                            payloadLengthReceived += buffer.Length;
                        }
                        else
                        {
                            await headerStream.WriteAsync(buffer.ToArray(), 0, (int)buffer.Length);

                            headerParsed = parseHeader(headerStream.ToArray(), out requestParameters);
                            Debug.WriteLine("hasHeader: " + headerParsed);

                            if (headerParsed)
                            {
                                uploadFileStream = await handleRequest(requestParameters, responseSender);

                                if (uploadFileStream != null)
                                {
                                    ulong length = buffer.Length - requestParameters.payloadOffset;
                                    if (length > 0)
                                    {
                                        IBuffer tempPayloadBuffer = headerStream.ToArray().AsBuffer(
                                            (int)requestParameters.payloadOffset, (int)length);
                                        await uploadFileStream.WriteAsync(tempPayloadBuffer);

                                        payloadLengthReceived += length;
                                    }
                                }
                            }
                        }

                        // End of request with a payload upload
                        if (uploadFileStream != null &&
                            payloadLengthReceived == requestParameters.payloadLength)
                        {
                            await responseSender.simpleOK();

                            // TODO: remove the file if it cannot be indexed?
                            bool success = await Locator.MediaLibrary.DiscoverMediaItemOrWaitAsync(
                                uploadFileStream.getStorageFile(), false);

                            if (success == false)
                            {
                                await uploadFileStream.getStorageFile().DeleteAsync();
                            }
                            break;
                        }

                        // End of request without required payload
                        if (uploadFileStream == null && headerParsed)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Http400Exception)
            {
                await responseSender.error400();
            }
            catch (Http404Exception)
            {
                await responseSender.error404();
            }
            catch (System.IO.FileNotFoundException)
            {
                await responseSender.error404();
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                // Assume the socket was closed, so don't send an error back
                return;
            }
            catch
            {
                await responseSender.error500();
            }
        }
Beispiel #3
0
        // Ham upload file khong su dung chu ky so
        public override void Execute()
        {
            Program.blCheckFile = false;
            base.Execute();
            var ctrlSA = new SAController();

            try
            {
                File.AppendAllText("LastErrors.log", string.Format("{0}\r\n-------------\r\n", DateTime.Now.ToLongTimeString()));
                checkedListFiles.Clear();
                for (int i = 0; i < checkedListBoxControl1.Items.Count; i++)
                {
                    if (checkedListBoxControl1.GetItemChecked(i) == true)
                    {
                        checkedListFiles.Add(checkedListBoxControl1.GetItemValue(i).ToString());
                    }
                }
                if (checkedListFiles.Count != 0)
                {
                    // Tao file Zip
                    var             outPathname = System.Environment.GetEnvironmentVariable("TEMP") + "\\" + RandomString(10, false) + ".zip";;
                    FileStream      fsOut       = File.Create(outPathname);
                    ZipOutputStream zipStream   = new ZipOutputStream(fsOut);
                    zipStream.SetLevel(9); //0-9, 9 being the highest level of compression
                    foreach (var filename in checkedListFiles)
                    {
                        if (!string.IsNullOrEmpty(filename))
                        {
                            FileInfo fi        = new FileInfo(filename);
                            string   entryName = System.IO.Path.GetFileName(filename);
                            entryName = ZipEntry.CleanName(entryName);
                            ZipEntry newEntry = new ZipEntry(entryName);
                            newEntry.DateTime = fi.LastWriteTime;

                            newEntry.Size = fi.Length;
                            zipStream.PutNextEntry(newEntry);

                            // Zip the file in buffered chunks
                            byte[] buffer = new byte[4096];
                            using (FileStream streamReader = File.OpenRead(filename))
                            {
                                StreamUtils.Copy(streamReader, zipStream, buffer);
                            }
                            zipStream.CloseEntry();
                        }
                    }
                    zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
                    zipStream.Close();
                    var _streamAttr = new FileStream(outPathname, FileMode.Open, FileAccess.Read);
                    prgUploadFile.Value = 0;
                    var fileUpload = new UploadFileStream(_streamAttr);
                    fileUpload.OnUploadStatusChanged += fileUpload_OnUploadStatusChanged;

                    FileUpload upload = new FileUpload();
                    upload.FileName     = System.IO.Path.GetFileName(outPathname);
                    upload.UploadStream = fileUpload;
                    upload.SecID        = 0;
                    ctrlSA.SaveFile(upload);

                    Program.FileName = Program.FileName + System.IO.Path.GetFileName(outPathname);
                    _streamAttr.Dispose();
                }

                //List<string> listparam = new List<string>();
                //for (int i = 0; i < checkedListBoxControl1.Items.Count; i++)
                //{

                //    if (checkedListBoxControl1.GetItemChecked(i) == false)
                //    {
                //        listparam.Clear();
                //        listparam.Add(checkedListBoxControl1.GetItemValue(i).ToString());
                //    }
                //}
                //FIS.Entities.Session session = new FIS.Entities.Session();
                //ctrlSA.GetCurrentSessionInfo(out session);
                //listparam.Add(session.SessionKey);
                //ctrlSA.ExecuteStoreProcedure("SP_DELFILE_BY_SESSIONSKEY", listparam);

                if (checkedListBoxControl1.Items.Count > 0)
                {
                    Program.blCheckFile = true;
                }

                File.AppendAllText("LastErrors.log", string.Format("{0}\r\n-------------\r\n", DateTime.Now.ToLongTimeString()));
                CloseModule();
            }
            catch (Exception ex)
            {
                //CREATE ERROCODE: 702 - ERR_FILE_IS_NOT_ATTACKED
                File.AppendAllText("LastErrors.log", string.Format("{0}\r\n-------------\r\n", ex.Message));
                Program.FileName = null;
                var cex = ErrorUtils.CreateError(702);
                ShowError(cex);
            }
        }