Example #1
0
        public async Task <IActionResult> Upload([FromForm] UploadPacket packet)
        {
            try
            {
                var files      = packet.Files;
                var folderName = Path.Combine("Resources", "Images");
                var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

                if (files.Any(f => f.Length == 0))
                {
                    return(BadRequest());
                }

                foreach (var file in files)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    var fullPath = Path.Combine(pathToSave, fileName);
                    var dbPath   = Path.Combine(folderName, fileName); //you can add this path to a list and then return all dbPaths to the client if require
                    using var stream = new FileStream(fullPath, FileMode.Create);
                    await file.CopyToAsync(stream);
                }
                return(Ok("All the files are successfully uploaded"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, $"Internal server error: {ex}"));
            }
        }
Example #2
0
        private void Socket_C()
        {
            Console.WriteLine("**** Socket_C ****");
            while (true)
            {
                string filename;
                if (stream.CanRead && stream.CanWrite)
                {
                    ///서버로부터 전송된 data 읽음
                    byte[] ReadByte;
                    ReadByte = new byte[client.ReceiveBufferSize];
                    int    BytesRead = stream.Read(ReadByte, 0, (int)ReadByte.Length);
                    string str       = Encoding.UTF8.GetString(ReadByte);
                    filename = Encoding.GetEncoding("utf-8").GetString(ReadByte, 0, BytesRead);

                    ///server와 연결되있는client의 name들 리스트뷰에 등록
                    if (str.Substring(0, 5) == "NAME#")
                    {
                        Console.WriteLine("add servername " + str);
                        string[] names = str.Split('/');
                        for (int i = 1; i < names.Length - 1; i++)
                        {
                            ListViewItem li = new ListViewItem();
                            li.Text = names[i];
                            li.SubItems.Add("");
                            li.SubItems.Add("");

                            Invoke((MethodInvoker) delegate
                            {
                                listView1.Items.Add(li);
                            });
                        }
                    }
                    ///추가로 연결되는 client의 name 리스트뷰에 등록
                    else if (str.Substring(0, 5) == "NAME*")
                    {
                        ListViewItem li = new ListViewItem();
                        li.Text = str.Substring(5, str.Length - 5);
                        li.SubItems.Add("");
                        li.SubItems.Add("");
                        Invoke((MethodInvoker) delegate
                        {
                            listView1.Items.Add(li);
                        });
                    }
                    ///다른 클라이언트의 lock값 변경
                    else if (str.Substring(0, 7) == "CHANGE@")
                    {
                        string[] info    = str.Split('/');
                        string   name    = info[1];
                        string   pptnum  = info[2];
                        string   pagenum = info[3];

                        Invoke((MethodInvoker) delegate
                        {
                            for (int i = 0; i < listView1.Items.Count; i++)
                            {
                                ListViewItem item = listView1.Items[i];
                                bool isContains   = item.SubItems[0].Text.Contains(name);
                                if (isContains)
                                {
                                    item.SubItems[1].Text = pptnum;
                                    item.SubItems[2].Text = pagenum;
                                    break;
                                }
                            }
                        });
                    }
                    ///lock실패했을경우
                    else if (str.Substring(0, 5) == "FAIL@")
                    {
                        MessageBox.Show("다른사용자가 편집중인 슬라이드입니다");
                    }
                    ///다른 사용자가 피피티를 편집했을 경우
                    else if (str.Substring(0, 5) == "EDIT@")
                    {
                        string[] info     = str.Split('/');
                        int      fileSize = Convert.ToInt32(info[1]);
                        int      pptnum   = Convert.ToInt32(info[2]);
                        int      pagenum  = Convert.ToInt32(info[3]);
                        bool     isadd;
                        if (info[4].CompareTo("True") == 0)
                        {
                            isadd = true;
                        }
                        else
                        {
                            isadd = false;
                        }


                        //변경할슬라이드가있는 피피티파일을 읽어와 'edit.pptx'생성후 저장
                        byte[]     myReadBuffer      = new byte[fileSize];
                        int        numberOfBytesRead = 0;
                        string     path = _namePath + @"\" + "edit.pptx";
                        FileStream fs   = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

                        numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
                        fs.Write(myReadBuffer, 0, numberOfBytesRead);


                        fs.Close();
                        PowerPoint.Slides tempSlides = presentation[pptnum].Slides;
                        if (isadd)
                        {
                            tempSlides.InsertFromFile(path, pagenum - 1, 1, 1);
                        }
                        else
                        {
                            tempSlides.InsertFromFile(path, pagenum, 1, 1);
                            tempSlides[pagenum].Delete();
                        }

                        File.Delete(path);
                    }
                    else if (str.Substring(0, 7) == "UPLOAD@")
                    {
                        if (filename != "")
                        {
                            ///upload시작한다고 server에게 전달 ///packetType = upload
                            byte[] buffer = new byte[1024 * 4];
                            uploadPacket      = new UploadPacket();
                            uploadPacket.type = (int)PacketType.UPLOAD;
                            uploadPacket.isup = true;
                            Packet.Serialize(uploadPacket).CopyTo(buffer, 0);
                            stream.Write(buffer, 0, buffer.Length);
                            Console.WriteLine("uploadpacket");

                            _namePath = _Path + @"\" + textBox_name.Text;
                            filename  = filename.Substring(7, filename.Length - 7);
                            Byte[] sendBytes = Encoding.GetEncoding("utf-8").GetBytes(_namePath + @"\" + filename);
                            stream.Write(sendBytes, 0, sendBytes.Length);

                            int    ByteSize      = 0;
                            Byte[] FileSizeBytes = new byte[client.ReceiveBufferSize];
                            ByteSize = stream.Read(FileSizeBytes, 0, FileSizeBytes.Length);
                            int MaxFileLength = Convert.ToInt32(Encoding.UTF8.GetString(FileSizeBytes, 0, ByteSize));

                            ///전송준비작업을 완료했다고 서버에 전해줌
                            byte[] ReadyTransBytes = new byte[client.ReceiveBufferSize];
                            ReadyTransBytes = Encoding.UTF8.GetBytes("READY");
                            stream.Write(ReadyTransBytes, 0, ReadyTransBytes.Length);


                            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(_namePath);
                            if (di.Exists == false)
                            {
                                di.Create();
                            }
                            FileStream fs = new FileStream(_namePath + @"\" + filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None);

                            if (filename != string.Empty)
                            {
                                byte[] myReadBuffer      = new byte[1024];
                                int    numberOfBytesRead = 0;

                                do
                                {
                                    numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
                                    fs.Write(myReadBuffer, 0, numberOfBytesRead);
                                }while (fs.Length < MaxFileLength);
                                //while (stream.DataAvailable);
                            }
                            fs.Flush();
                            fs.Close();
                            stream.Flush();

                            Invoke((MethodInvoker) delegate
                            {
                                ButtonPPT[IdxPPT].Visible = true;
                                ButtonPPT[IdxPPT].Enabled = true;
                                ButtonPPT[IdxPPT].Tag     = _namePath + @"\" + filename;
                                LabelPPT[IdxPPT].Visible  = true;
                                LabelPPT[IdxPPT].Text     = Path.GetFileNameWithoutExtension(filename);

                                IdxPPT++;
                            });
                        }
                    }
                }
            }
        }