PatchFileUploader.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using DevExpress.Data.Filtering.Helpers;
  2. using System;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace PatchFileUploader
  9. {
  10. public partial class PatchFileUploader : Form
  11. {
  12. private const string CONNECTION_STRING = "Server=sf.tascorp.co.kr,1444;database=_GIT_MIRROR_DB;uid=sa;pwd=!1Tascorp";
  13. private string _UploadFilePath = string.Empty;
  14. public PatchFileUploader()
  15. {
  16. InitializeComponent();
  17. SearchFileList();
  18. }
  19. private void SearchFileList()
  20. {
  21. using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
  22. {
  23. conn.Open();
  24. string query = @"
  25. SELECT FileName, FileVersion, IsCurrentYN, CONVERT(CHAR(23), UploadTime, 21) AS UploadTime
  26. FROM Patch
  27. ORDER BY id
  28. ";
  29. SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
  30. DataTable fileData = new DataTable();
  31. adapter.Fill(fileData);
  32. gc_File.DataSource = fileData;
  33. }
  34. }
  35. private void btn_SelectFile_Click(object sender, EventArgs e)
  36. {
  37. OpenFileDialog openFileDialog = new OpenFileDialog();
  38. openFileDialog.Filter = "Zip files (*.zip)|*.zip";
  39. if (DialogResult.OK == openFileDialog.ShowDialog())
  40. {
  41. _UploadFilePath = openFileDialog.FileName;
  42. tb_FileName.Text = Path.GetFileName(_UploadFilePath);
  43. }
  44. }
  45. private void btn_Upload_Click(object sender, EventArgs e)
  46. {
  47. if (_UploadFilePath == string.Empty)
  48. {
  49. MessageBox.Show("업로드할 파일이 선택되지 않았습니다.");
  50. return;
  51. }
  52. if (tb_Version.Text == string.Empty)
  53. {
  54. MessageBox.Show("파일 버전이 입력되지 않았습니다.");
  55. return;
  56. }
  57. using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
  58. {
  59. conn.Open();
  60. try
  61. {
  62. string query = @"
  63. INSERT INTO Patch (FileName, FileContent, FileVersion, IsCurrentYN)
  64. VALUES (@FILE_NAME, @FILE_CONTENT, @FILE_VERSION, 'Y')
  65. UPDATE Patch
  66. SET IsCurrentYN = 'N'
  67. WHERE FileVersion <> @FILE_VERSION
  68. ";
  69. SqlCommand sqlCommand = new SqlCommand(query, conn);
  70. sqlCommand.Parameters.AddWithValue("@FILE_NAME", Path.GetFileName(_UploadFilePath));
  71. sqlCommand.Parameters.AddWithValue("@FILE_VERSION", tb_Version.Text);
  72. sqlCommand.Parameters.AddWithValue("@FILE_CONTENT", GetFileContent(_UploadFilePath));
  73. sqlCommand.ExecuteNonQuery();
  74. SearchFileList();
  75. MessageBox.Show("업로드가 완료 되었습니다.");
  76. _UploadFilePath = string.Empty;
  77. tb_FileName.Text = string.Empty;
  78. }
  79. catch (Exception ex)
  80. {
  81. MessageBox.Show(ex.Message);
  82. }
  83. }
  84. }
  85. private byte[] GetFileContent(string filePath)
  86. {
  87. FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  88. BinaryReader br = new BinaryReader(fs);
  89. byte[] fileContent = br.ReadBytes((int)fs.Length);
  90. br.Close();
  91. fs.Close();
  92. return fileContent;
  93. }
  94. private void gv_File_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
  95. {
  96. tb_Version.Text = gv_File.GetFocusedRowCellValue(gc_FileVersion).ToString();
  97. }
  98. private void btn_Change_Click(object sender, EventArgs e)
  99. {
  100. using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
  101. {
  102. conn.Open();
  103. try
  104. {
  105. string query = @"
  106. UPDATE Patch
  107. SET IsCurrentYN = IIF(FileVersion = @FILE_VERSION, 'Y', 'N')
  108. ";
  109. SqlCommand sqlCommand = new SqlCommand(query, conn);
  110. sqlCommand.Parameters.AddWithValue("@FILE_VERSION", gv_File.GetFocusedRowCellValue(gc_FileVersion).ToString());
  111. sqlCommand.ExecuteNonQuery();
  112. SearchFileList();
  113. MessageBox.Show("변경이 완료 되었습니다.");
  114. }
  115. catch (Exception ex)
  116. {
  117. MessageBox.Show(ex.Message);
  118. }
  119. }
  120. }
  121. private void btn_Delete_Click(object sender, EventArgs e)
  122. {
  123. using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
  124. {
  125. conn.Open();
  126. try
  127. {
  128. string query = @"
  129. DELETE Patch
  130. WHERE FileVersion = @FILE_VERSION
  131. ";
  132. SqlCommand sqlCommand = new SqlCommand(query, conn);
  133. sqlCommand.Parameters.AddWithValue("@FILE_VERSION", gv_File.GetFocusedRowCellValue(gc_FileVersion).ToString());
  134. sqlCommand.ExecuteNonQuery();
  135. SearchFileList();
  136. MessageBox.Show("삭제가 완료 되었습니다.");
  137. }
  138. catch (Exception ex)
  139. {
  140. MessageBox.Show(ex.Message);
  141. }
  142. }
  143. }
  144. private void btn_Download_Click(object sender, EventArgs e)
  145. {
  146. FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
  147. if (DialogResult.OK == folderBrowserDialog.ShowDialog())
  148. {
  149. using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
  150. {
  151. conn.Open();
  152. try
  153. {
  154. string query = @"
  155. SELECT FileName, FileContent
  156. FROM Patch
  157. WHERE FileVersion = '@FILE_VERSION'
  158. ";
  159. query = query.Replace("@FILE_VERSION", gv_File.GetFocusedRowCellValue(gc_FileVersion).ToString());
  160. SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
  161. DataTable fileData = new DataTable();
  162. adapter.Fill(fileData);
  163. DataRow dataRow = fileData.Rows[0];
  164. string fileName = dataRow["FileName"].ToString();
  165. byte[] fileContent = (byte[])dataRow["FileContent"];
  166. // 파일이 존재하는지 확인하고, 존재하지 않으면 파일 생성
  167. string filePath = Path.Combine(folderBrowserDialog.SelectedPath, fileName);
  168. if (!File.Exists(filePath))
  169. File.WriteAllBytes(filePath, fileContent);
  170. else
  171. {
  172. // 파일 내용 비교 후 다를 경우 파일 생성
  173. byte[] existingFileContent = File.ReadAllBytes(filePath);
  174. if (!fileContent.SequenceEqual(existingFileContent))
  175. File.WriteAllBytes(filePath, fileContent);
  176. }
  177. MessageBox.Show("다운로드가 완료 되었습니다.");
  178. }
  179. catch (Exception ex)
  180. {
  181. MessageBox.Show(ex.Message);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }