Exemple #1
0
        private void FirstConnectSync()      //채티방 처음 입장 시 업로드, 다운로드, 삭제 동기화 함수
        {
            while (true)
            {
                int firstSyncErr = 0;

                cnt = 0;

                try
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(cloudDirPath);

                    ////////////////////////파일 목록 전송 시작////////////////////////
                    JsonObjectCollection jsonObject  = new JsonObjectCollection();
                    JsonObjectCollection filesObject = new JsonObjectCollection("files");

                    jsonObject.Add(new JsonStringValue("userId", UserInfo.userId));
                    jsonObject.Add(new JsonStringValue("roomId", roomId));

                    foreach (var file in directoryInfo.GetFiles())
                    {
                        fileLastWriteTime = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                        fileLength        = file.Length.ToString();

                        if (file.Name.Equals("Thumbs.db") || Regex.IsMatch(file.Extension, @"\.db", RegexOptions.IgnoreCase))
                        {
                            Debug.WriteLine("<Chat_Form> .db OR Thumbs.db");
                        }
                        else
                        {
                            JsonObjectCollection filesInfoObject = new JsonObjectCollection(cnt.ToString());
                            filesInfoObject.Add(new JsonStringValue("fileName", file.Name));
                            filesInfoObject.Add(new JsonStringValue("fileExtension", file.Extension));
                            filesInfoObject.Add(new JsonStringValue("fileLastWriteTime", fileLastWriteTime));
                            filesInfoObject.Add(new JsonStringValue("fileLength", fileLength));

                            filesObject.Add(filesInfoObject);

                            cnt++;
                        }
                    }
                    jsonObject.Add(new JsonStringValue("length", cnt.ToString()));
                    jsonObject.Add(filesObject);

                    jsonString = jsonObject.ToString();

                    JsonSend jsonSend = new JsonSend();
                    jsonResult = jsonSend.JsonSendToServer(jsonString, 1);

                    Debug.WriteLine("<Chat_Form> upload json send : " + jsonString);

                    Debug.WriteLine("<Chat_Form> upload json Result : " + jsonResult);
                    ////////////////////////파일 목록 전송 완료////////////////////////

                    ////////////////////////서버에 없는 파일 전송 시작////////////////////////
                    JObject jObject = JObject.Parse(jsonResult);

                    NameValueCollection updateFileList = new NameValueCollection();

                    jsonLength     = jObject["uploadSize"].ToString();
                    fileListLength = Convert.ToInt32(jsonLength);

                    jsonDownloadLength = jObject["downloadSize"].ToString();
                    jsonDeleteLength   = jObject["deleteListSize"].ToString();

                    downloadListLength = Convert.ToInt32(jsonDownloadLength);
                    deleteListLength   = Convert.ToInt32(jsonDeleteLength);

                    ////////////////////////삭제 파일 리스트 로컬에서 삭제////////////////////
                    if (deleteListLength > 0)
                    {
                        NameValueCollection deleteFileList = new NameValueCollection();

                        for (int i = 0; i < deleteListLength; i++)
                        {
                            updateFileList.Add(Convert.ToString(i), jObject["deleteList"][i].ToString());
                        }

                        while (true)
                        {
                            try
                            {
                                for (int i = 0; i < deleteListLength; i++)
                                {
                                    File.Delete(cloudDirPath + updateFileList[i]);
                                }
                                Debug.WriteLine("파일 삭제 리스트 삭제 완료");
                                deleteErrCnt = 0;
                                break;
                            }
                            catch (Exception ex)
                            {
                                deleteErrCnt++;
                                Debug.WriteLine("파일 삭제 리스트 삭제 오류 : " + ex);

                                if (deleteErrCnt == 5)
                                {
                                    deleteErrCnt = 0;
                                    Debug.WriteLine("deleteErrCnt==5");
                                    break;
                                }
                                throw;
                            }
                        }
                    }
                    ////////////////////////삭제 파일 리스트 로컬에서 삭제 완료////////////////////

                    ////////////////////////서버에 없는 파일 전송 시작////////////////////
                    for (int i = 0; i < fileListLength; i++)
                    {
                        updateFileList.Add(Convert.ToString(i), jObject["uploadList"][i].ToString());
                    }

                    for (int i = 0; i < fileListLength; i++)
                    {
                        updateFileName = cloudDirPath + updateFileList[i];
                        string eachFileName = updateFileList[i];

                        Debug.WriteLine("<Chat_Form> updateFileName : " + updateFileName);

                        fileInfo      = new FileInfo(updateFileName);
                        fileExtension = Path.GetExtension(updateFileName);

                        if (Regex.IsMatch(fileExtension, @"\.lnk", RegexOptions.IgnoreCase))
                        {
                            MessageBox.Show(".lnk 파일은 동기화 할 수 없습니다.");
                        }
                        else if (Regex.IsMatch(fileExtension, @"\.tmp", RegexOptions.IgnoreCase) || fileInfo.Name.Contains("~$"))
                        {
                            Debug.WriteLine("<Chat_Form> 임시파일 전송 불가 : " + fileInfo.Name);
                        }
                        else if (fileInfo.Name.Contains("~$") && Regex.IsMatch(fileExtension, @"\.Cell", RegexOptions.IgnoreCase))
                        {
                            Debug.WriteLine("<Chat_Form> 임시파일 전송 불가 : " + fileInfo.Name);
                        }
                        else if (fileInfo.Name.Contains("~$"))
                        {
                            Debug.WriteLine("<Chat_Form> 임시파일 전송 불가 : " + fileInfo.Name);
                        }
                        else
                        {
                            while (true)
                            {
                                try
                                {
                                    fileLastWriteTime = fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                                    length            = fileInfo.Length;
                                    fileName          = fileInfo.Name;

                                    fileStream = File.Open(updateFileName, FileMode.Open, FileAccess.Read);
                                    fileStream.Close();
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    if (openFileErr == 5)
                                    {
                                        Debug.WriteLine("openFileErr == 5");
                                        break;
                                    }
                                    else if (ex.ToString().Contains("System.IO.FileNotFoundException"))
                                    {
                                        Debug.WriteLine("파일을 찾을 수 없습니다.");
                                        break;
                                    }
                                    Debug.WriteLine("열려 있는 파일을 닫아야합니다.");
                                    openFileErr++;
                                    Thread.Sleep(2000);
                                }
                            }

                            Watcher.SendFileUpload(updateFileName, fileLastWriteTime, length, fileName, fileExtension);


                            simpleData.runThread(0, eachFileName); // 파일 다운로드 소켓 명령
                        }
                    }
                    Debug.WriteLine("-------------<Chat_Form> 파일 전송 완료----------------");
                    ////////////////////////서버에 없는 파일 전송 완료////////////////////////

                    ////////////////////////서버에 있고 공유 폴더에 없는 파일 목록 전송 시작////////////////////////
                    if (downloadListLength > 0)
                    {
                        JsonObjectCollection jsonDownloadObject  = new JsonObjectCollection();
                        JsonArrayCollection  jsonArrayCollection = new JsonArrayCollection("uploadList");

                        jsonDownloadObject.Add(new JsonStringValue("userId", UserInfo.userId));
                        jsonDownloadObject.Add(new JsonStringValue("roomId", roomId));

                        for (int i = 0; i < downloadListLength; i++)
                        {
                            jsonArrayCollection.Add(new JsonStringValue(null, jObject["downloadList"][i].ToString()));
                        }

                        jsonDownloadObject.Add(jsonArrayCollection);
                        jsonString = jsonDownloadObject.ToString();
                        jsonResult = jsonSend.JsonSendToServer(jsonString, 2);

                        Debug.WriteLine("<Chat_Form> download json send : " + jsonString);
                        Debug.WriteLine("<Chat_Form> download json response : " + jsonResult);

                        ////////////////////////서버로부터 zip파일 전송 받기 시작////////////////////////
                        if (jsonResult.Equals("created zip file"))
                        {
                            ZipFileDownload();
                        }
                        ////////////////////////서버로부터 zip파일 전송 받기 완료////////////////////////
                    }

                    break;
                }
                catch (Exception ex)
                {
                    firstSyncErr++;
                    Debug.WriteLine("FirstSync Err : " + ex);

                    Thread.Sleep(2000);

                    if (firstSyncErr == 5)
                    {
                        Debug.WriteLine("firstSyncErr==5");
                        break;
                    }
                }
                break;
            }
        }
Exemple #2
0
        // 감시 폴더 내부 변경 event
        private void Watcher_Created(object sender, FileSystemEventArgs e)
        {
            fileLocation = e.FullPath;

            fileInfo = new FileInfo(fileLocation);

            string createdFileExtension = Path.GetExtension(fileLocation);

            if (Regex.IsMatch(createdFileExtension, @"\.lnk", RegexOptions.IgnoreCase))
            {
                MessageBox.Show(".lnk 파일은 동기화 할 수 없습니다.");
            }
            else if (Regex.IsMatch(createdFileExtension, @"\.tmp", RegexOptions.IgnoreCase))
            {
                Debug.WriteLine("<Watcher> 임시파일 생성 : " + fileInfo.Name);
            }
            else if (fileInfo.Name.Contains("~$") && Regex.IsMatch(createdFileExtension, @"\.Cell", RegexOptions.IgnoreCase))
            {
                Debug.WriteLine("<Watcher> 임시파일 생성 : " + fileInfo.Name);
            }
            else if (fileInfo.Name.Contains("~$"))
            {
                Debug.WriteLine("<Watcher> 임시파일 생성 : " + fileInfo.Name);
            }
            else if (createdFileExtension.Equals(""))
            {
                Debug.WriteLine("<Watcher> 임시파일 생성 : " + fileInfo.Name);
            }
            else if (Regex.IsMatch(createdFileExtension, @"\.db", RegexOptions.IgnoreCase))
            {
                Debug.WriteLine("<Watcher> .db 파일 생성 : " + fileInfo.Name);
            }
            else if (fileInfo.Name.Equals("Thumbs.db"))
            {
                Debug.WriteLine("<Watcher> 임시파일 생성 : " + fileInfo.Name);
            }
            else
            {
                int cnt = 0;

                while (true)
                {
                    try
                    {
                        fileLastWriteTime = fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                        fileLength        = fileInfo.Length;
                        fileName          = fileInfo.Name;

                        fileStream = File.Open(fileLocation, FileMode.Open, FileAccess.Read);
                        fileStream.Close();

                        fileOpen = false;

                        break;
                    }
                    catch (Exception ex)
                    {
                        cnt++;
                        if (ex.ToString().Contains("System.IO.FileNotFoundException"))
                        {
                            Debug.WriteLine("파일을 찾을 수 없습니다.");
                            break;
                        }
                        Debug.WriteLine("Watcher Created Upload Error : " + ex);
                        Thread.Sleep(3000);

                        fileOpen = true;

                        if (cnt == 5)
                        {
                            Debug.WriteLine("Watcher Created Upload Error CNT==5");
                            break;
                        }
                    }
                }

                if (fileOpen == false)
                {
                    JsonObjectCollection jsonObject = new JsonObjectCollection();
                    jsonObject.Add(new JsonStringValue("roomId", roomId));
                    jsonObject.Add(new JsonStringValue("fileName", fileName));
                    jsonObject.Add(new JsonStringValue("fileLastWriteTime", fileLastWriteTime));

                    jsonString = jsonObject.ToString();

                    JsonSend jsonSend = new JsonSend();
                    jsonResult = jsonSend.JsonSendToServer(jsonString, 4);

                    if (jsonResult == null)
                    {
                        Debug.WriteLine("<Watcher> created jsonResult Err NULL : " + jsonResult);
                    }
                    else if (jsonResult.Equals("true"))
                    {
                        Debug.WriteLine("<Watcher> created json send : " + jsonString);
                        Debug.WriteLine("<Watcher>제이슨 서버 존재 유무 판단 created json Result : " + jsonResult);

                        SendFileUpload(fileLocation, fileLastWriteTime, fileLength, fileName, createdFileExtension);
                        Debug.WriteLine("<Watcher> 생성 : " + fileName);

                        simpleData.runThread(0, fileName);
                        //chat_Form.sendSocket("[파일 다운로드 명령]/" + fileName+"/");

                        System.Timers.Timer timer = new System.Timers.Timer();
                        ((FileSystemWatcher)sender).Changed -= Watcher_Changed;
                        timer.Interval = 1000;
                        timer.Elapsed += new ElapsedEventHandler(EventTimer);
                        timer.Start();
                    }
                    else
                    {
                        Debug.WriteLine("<Watcher> created json send : " + jsonString);
                        Debug.WriteLine("<Watcher> created json Result : " + jsonResult);
                    }
                }
            }
        }
Exemple #3
0
        public static void ZipFileDownload()     //알집 파일 다운로드, 압축 해체 및 삭제 함수
        {
            zipFileName = JsonSend.zipPath;
            Debug.WriteLine(zipFileName);
            while (true)
            {
                int zipFileStreamErr = 0;

                try
                {
                    fileStream = File.Open(zipFileName, FileMode.Open, FileAccess.Read);
                    fileStream.Close();
                    break;
                }
                catch
                {
                    zipFileStreamErr++;

                    Thread.Sleep(2000);
                    Debug.WriteLine("Zip File 사용중 : " + zipFileStreamErr);

                    if (zipFileStreamErr == 5)
                    {
                        Debug.WriteLine("zipFileStreamErr==5");
                        zipFileStreamErr = 0;
                        break;
                    }
                }
            }

            NameValueCollection zipFileList = new NameValueCollection();

            while (true)
            {
                try
                {
                    using (ZipArchive zipFile = ZipFile.OpenRead(zipFileName))
                    {
                        zipFileCount = zipFile.Entries.Count;
                        Debug.WriteLine("zipFileCount : " + zipFileCount);

                        foreach (ZipArchiveEntry entry in zipFile.Entries)
                        {
                            Debug.WriteLine("ENTRY NAME : " + entry.FullName);
                            entry.ExtractToFile(cloudDirPath + entry.FullName, true);
                            byte[] buffer = File.ReadAllBytes(cloudDirPath + entry.FullName);
                            Key.BCapsulation(UserInfo.Key, buffer);
                            File.WriteAllBytes(cloudDirPath + entry.FullName, buffer);

                            zipFileList.Add(Convert.ToString(entry), entry.FullName);
                            Debug.WriteLine("zipFileList : " + zipFileList[Convert.ToString(entry)]);
                        }

                        Debug.WriteLine("압축 해제 완료");

                        zipErrCnt = 0;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    zipErrCnt++;

                    Debug.WriteLine("압축 해제 오류 : " + ex);
                    Thread.Sleep(2000);

                    if (zipErrCnt == 5)
                    {
                        zipErrCnt = 0;

                        Debug.WriteLine("zipErrCnt==5");
                        break;
                    }
                }
            }

            JsonObjectCollection jsonDownloadFinishObject = new JsonObjectCollection();
            JsonArrayCollection  jsonArrayCollection      = new JsonArrayCollection("uploadList");

            jsonDownloadFinishObject.Add(new JsonStringValue("userId", UserInfo.userId));
            jsonDownloadFinishObject.Add(new JsonStringValue("roomId", roomId));
            jsonDownloadFinishObject.Add(new JsonStringValue("result", "true"));

            for (int i = 0; i < zipFileCount; i++)
            {
                Debug.WriteLine("zipFileList : " + zipFileList[i]);
                jsonArrayCollection.Add(new JsonStringValue(null, zipFileList[i]));
            }

            jsonDownloadFinishObject.Add(jsonArrayCollection);

            jsonString = jsonDownloadFinishObject.ToString();

            Debug.WriteLine("압축 해제 완료 JSON 전송 : " + jsonString);
            JsonSend jsonSend = new JsonSend();

            jsonResult = jsonSend.JsonSendToServer(jsonString, 3);
            Debug.WriteLine("압축 해제 완료 JSON RESULT : " + jsonResult);

            JObject jObject = JObject.Parse(jsonResult);

            NameValueCollection fileListName  = new NameValueCollection();
            NameValueCollection lastWriteTime = new NameValueCollection();

            jsonLength     = jObject["length"].ToString();
            fileListLength = Convert.ToInt32(jsonLength);

            for (int i = 0; i < fileListLength; i++)
            {
                Debug.WriteLine("JSON FileName : " + jObject[Convert.ToString(i)]["FileName"].ToString());
                Debug.WriteLine("JSON Time : " + jObject[Convert.ToString(i)]["Time"].ToString());
                fileListName.Add(Convert.ToString(i), jObject[Convert.ToString(i)]["FileName"].ToString());
                lastWriteTime.Add(Convert.ToString(i), jObject[Convert.ToString(i)]["Time"].ToString());
            }

            DateTime dateTime = new DateTime();


            while (true)
            {
                try
                {
                    for (int i = 0; i < fileListLength; i++)
                    {
                        dateTime = Convert.ToDateTime(lastWriteTime[i]);
                        File.SetLastWriteTime(cloudDirPath + fileListName[i], dateTime);
                    }
                    zipErrCnt = 0;
                    break;
                }
                catch (Exception ex)
                {
                    zipErrCnt++;

                    Debug.WriteLine("파일 수정 날짜 갱신 오류 : " + ex);
                    Thread.Sleep(2000);

                    if (zipErrCnt == 5)
                    {
                        zipErrCnt = 0;

                        Debug.WriteLine("zipErrCnt==5");
                        break;
                    }
                }
            }

            try
            {
                FileInfo fileDel = new FileInfo(zipFileName);
                fileDel.Delete();

                Debug.WriteLine("압축 파일 삭제 완료 : " + zipFileName);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("압축 파일 삭제 Error : " + ex);
            }
        }
Exemple #4
0
        private void Watcher_Changed(object sender, FileSystemEventArgs e)
        {
            string changedFileExtension;

            fileLocation = e.FullPath;

            fileInfo = new FileInfo(fileLocation);
            System.Timers.Timer timer = new System.Timers.Timer();

            ((FileSystemWatcher)sender).Changed -= Watcher_Changed;
            timer.Interval = 1000;
            timer.Elapsed += new ElapsedEventHandler(EventTimer);
            timer.Start();

            try
            {
                changedFileExtension = Path.GetExtension(fileLocation);

                if (Regex.IsMatch(changedFileExtension, @"\.lnk", RegexOptions.IgnoreCase))
                {
                    Debug.WriteLine("<Watcher> .lnk 파일 변경 : " + fileInfo.Name);
                }
                else if (Regex.IsMatch(changedFileExtension, @"\.tmp", RegexOptions.IgnoreCase))
                {
                    Debug.WriteLine("<Watcher> 임시파일 변경 : " + fileInfo.Name);
                }
                else if (fileInfo.Name.Contains("~$"))
                {
                    Debug.WriteLine("<Watcher> 임시파일 변경 : " + fileInfo.Name);
                }
                else if (changedFileExtension.Equals(""))
                {
                    Debug.WriteLine("<Watcher> 임시파일 변경 : " + fileInfo.Name);
                }
                else if (Regex.IsMatch(changedFileExtension, @"\.db", RegexOptions.IgnoreCase))
                {
                    Debug.WriteLine("<Watcher> .db 파일 변경 : " + fileInfo.Name);
                }
                else if (fileInfo.Name.Equals("Thumbs.db"))
                {
                    Debug.WriteLine("<Watcher> 임시파일 변경 : " + fileInfo.Name);
                }
                else
                {
                    fileLastWriteTime = fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                    fileLength        = fileInfo.Length;
                    fileName          = fileInfo.Name;

                    JsonObjectCollection jsonObject = new JsonObjectCollection();
                    jsonObject.Add(new JsonStringValue("roomId", roomId));
                    jsonObject.Add(new JsonStringValue("fileName", fileName));
                    jsonObject.Add(new JsonStringValue("fileLastWriteTime", fileLastWriteTime));
                    jsonObject.Add(new JsonStringValue("fileSize", fileLength.ToString()));

                    jsonString = jsonObject.ToString();

                    JsonSend jsonSend = new JsonSend();
                    jsonResult = jsonSend.JsonSendToServer(jsonString, 5);

                    if (jsonResult == null)
                    {
                        Debug.WriteLine("<Watcher> 변경 jsonResult Err NULL : " + jsonResult);
                    }
                    else if (jsonResult.Equals("true"))
                    {
                        SendFileUpload(fileLocation, fileLastWriteTime, fileLength, fileName, changedFileExtension);
                        Debug.WriteLine("<Watcher> 변경 : " + e.Name);

                        Debug.WriteLine("<Watcher> 변경 json send : " + jsonString);
                        Debug.WriteLine("<Watcher> 변경 json Result : " + jsonResult);

                        simpleData.runThread(3, fileName);
                        //chat_Form.sendSocket("[파일 변경 명령]/" + fileName);
                    }
                    else
                    {
                        Debug.WriteLine("<Watcher> 변경 json send : " + jsonString);
                        Debug.WriteLine("<Watcher> 변경 json Result : " + jsonResult);
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine("<Watcher> Changed Error : " + exception);
            }
        }