487 lines
15 KiB
C#
487 lines
15 KiB
C#
using edu.huvtc.utils;
|
|
using FileProcessClient.domain;
|
|
using FileProcessClient.utils;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Diagnostics;
|
|
using System.Security.AccessControl;
|
|
|
|
namespace FileProcessClient
|
|
{
|
|
public partial class mainForm : Form
|
|
{
|
|
private static List<string> uploadFiles;
|
|
|
|
private static string remotePath;
|
|
|
|
private static bool isUploading = false;
|
|
|
|
public mainForm()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void btnUpload_MouseHover(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void btnOpen_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void mainForm_FormClosed(object sender, FormClosedEventArgs e)
|
|
{
|
|
Application.Exit();
|
|
}
|
|
|
|
//private
|
|
|
|
private void btnSelDirectory_Click(object sender, EventArgs e)
|
|
{
|
|
addDirectoryToList(0);
|
|
}
|
|
|
|
private void changeButtonState(int status)
|
|
{
|
|
switch (status)
|
|
{
|
|
case 1:
|
|
btnUpload.BackgroundImage = Properties.Resources.upload_to_server;
|
|
btnUpload.Enabled = true;
|
|
break;
|
|
case 2:
|
|
btnUpload.BackgroundImage = Properties.Resources.upload_to_server_01;
|
|
btnUpload.Enabled = false;
|
|
break;
|
|
case 3:
|
|
btnSelDirectory.Enabled = false;
|
|
btnSelFile.Enabled = false;
|
|
btnUpload.Enabled = false;
|
|
uploadbar.Visible = true;
|
|
break;
|
|
case 4:
|
|
btnSelDirectory.Enabled = true;
|
|
btnSelFile.Enabled = true;
|
|
btnUpload.Enabled = false;
|
|
uploadbar.Visible = false;
|
|
break;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
private void addToTable(List<string> list)
|
|
{
|
|
clearTable();
|
|
foreach (var item in list)
|
|
{
|
|
int index = fileListGrid.Rows.Add();
|
|
fileListGrid.Rows[index].Cells[0].Value = index + 1;
|
|
fileListGrid.Rows[index].Cells[1].Value = item;
|
|
fileListGrid.Rows[index].Cells[2].Value = "待上传";
|
|
}
|
|
|
|
}
|
|
|
|
private void clearTable()
|
|
{
|
|
while (fileListGrid.Rows.Count != 0)
|
|
{
|
|
fileListGrid.Rows.RemoveAt(0);
|
|
}
|
|
}
|
|
|
|
private async void loadCameraData()
|
|
{
|
|
List<ComboboxItem> list = await HttpUtils.getCameraList();
|
|
foreach (ComboboxItem item in list)
|
|
{
|
|
cameraList.Items.Add(item);
|
|
}
|
|
list = await HttpUtils.getVersionList();
|
|
foreach (ComboboxItem item in list)
|
|
{
|
|
|
|
versionList.Items.Add(item);
|
|
}
|
|
}
|
|
|
|
private void getAllMediaFile(string path, List<string> list)
|
|
{
|
|
try
|
|
{
|
|
DirectoryInfo dir = new DirectoryInfo(path);
|
|
|
|
var allowedExtensions = new[] { ".mov", ".mp4", ".mkv", ".rmvb", ".json",".jpg",".png" };
|
|
var files = Directory
|
|
.GetFiles(path)
|
|
.Where(file => allowedExtensions.Any(file.ToLower().EndsWith))
|
|
.ToList();
|
|
foreach (string file in files)
|
|
{
|
|
list.Add(file);
|
|
}
|
|
|
|
// Get all subdirectories
|
|
DirectoryInfo[] subDirs = dir.GetDirectories();
|
|
foreach (DirectoryInfo subDir in subDirs)
|
|
{
|
|
// Recursively list files in subdirectories
|
|
getAllMediaFile(subDir.FullName, list);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"An error occurred: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void showFileUploadProgress(int index)
|
|
{
|
|
if (uploadFiles == null || uploadFiles.Count == 0)
|
|
{
|
|
uploadbar.Value = 0;
|
|
}
|
|
else
|
|
{
|
|
int percent = (index * 100) / uploadFiles.Count;
|
|
uploadbar.Value = percent;
|
|
}
|
|
}
|
|
|
|
private void processResponse(int index, int i)
|
|
{
|
|
showFileUploadProgress(index);
|
|
//Trace.WriteLine(i);
|
|
if (i == -1)
|
|
{
|
|
fileListGrid.Rows[index].Cells[2].Value = "上传失败";
|
|
}
|
|
else if (i < 100)
|
|
{
|
|
fileListGrid.Rows[index].Cells[2].Value = "已上传" + i + "%";
|
|
}
|
|
else
|
|
{
|
|
fileListGrid.Rows[index].Cells[2].Value = "已完成";
|
|
}
|
|
}
|
|
|
|
private void mainForm_Load(object sender, EventArgs e)
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
//txtVersion.Text = now.ToString("yyyyMMddHHmmss");
|
|
lblUserName.Text = CommonUtils.getUser()!.Name;
|
|
btnOpenUpload_Click(sender, e);
|
|
loadCameraData();
|
|
loadConfigData();
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnUpload_Click(object sender, EventArgs e)
|
|
{
|
|
doUploadFiles();
|
|
}
|
|
|
|
private async void doUploadFiles()
|
|
{
|
|
if (uploadFiles == null || uploadFiles.Count == 0)
|
|
{
|
|
MessageBox.Show("请选择要上传的文件或目录");
|
|
return;
|
|
}
|
|
var item = cameraList.SelectedItem;
|
|
if (item == null)
|
|
{
|
|
MessageBox.Show("请选择所属相机");
|
|
return;
|
|
}
|
|
|
|
var versionItem = versionList.SelectedItem;
|
|
if (versionItem == null)
|
|
{
|
|
string str = versionList.Text;
|
|
if(str==null || str.Length == 0)
|
|
{
|
|
MessageBox.Show("请选择或新增版本号");
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
versionItem = new ComboboxItem(str, 0);
|
|
}
|
|
}
|
|
|
|
if (versionItem.GetType() == typeof(string))
|
|
{
|
|
string value = (string)versionItem;
|
|
versionItem = new ComboboxItem(value, 0);
|
|
}
|
|
|
|
changeButtonState(3);
|
|
|
|
ComboboxItem item1 = (ComboboxItem)item;
|
|
ComboboxItem item2 = (ComboboxItem)versionItem;
|
|
|
|
string remotePath = "/" + item1.Text + "/" + item2.Text;
|
|
|
|
//string remotePath = item1.Text + "/" + item2.Text;
|
|
//Trace.Write("==================" + remotePath);
|
|
|
|
List<MediaFileItem> list = getMediaFileItemList(uploadFiles, remotePath);
|
|
|
|
bool flag = await FileUploadUtils.UploadFilesAsync(list, remotePath, processResponse);
|
|
|
|
if (flag)
|
|
{
|
|
MediaFileInfo fileInfo = new MediaFileInfo();
|
|
fileInfo.fileItems = list;
|
|
fileInfo.deviceId = item1.Value.ToString()!;
|
|
fileInfo.deviceName = item1.Text;
|
|
fileInfo.version = item2.Text;
|
|
//Trace.WriteLine("==============="+item2.Value);
|
|
string str = (string)item2.Value;
|
|
fileInfo.versionId = int.Parse(str==null?"0": str);
|
|
//Trace.WriteLine("========"+ fileInfo.versionId);
|
|
fileInfo.remotePath = remotePath;
|
|
await HttpUtils.saveMediaFileInfo(fileInfo);
|
|
changeButtonState(4);
|
|
}
|
|
}
|
|
|
|
private bool isMediaFile(String name)
|
|
{
|
|
if (name.ToLower().EndsWith(".mov") || name.ToLower().EndsWith(".mp4") || name.ToLower().EndsWith(".jpg") || name.ToLower().EndsWith(".png") || name.ToLower().EndsWith(".bmp"))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private bool isValidFile(String name)
|
|
{
|
|
if (name.ToLower().EndsWith(".json") || name.ToLower().EndsWith(".mov") || name.ToLower().EndsWith(".mp4") || name.ToLower().EndsWith(".jpg") || name.ToLower().EndsWith(".png") || name.ToLower().EndsWith(".bmp"))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
private List<MediaFileItem> getMediaFileItemList(List<string> uploadFiles, string remotePath)
|
|
{
|
|
//remotePath = "/data5/file/xuebao" + remotePath;
|
|
List<MediaFileItem> list = new List<MediaFileItem>();
|
|
foreach (string name in uploadFiles)
|
|
{
|
|
MediaFileItem item = new MediaFileItem();
|
|
//item.fileType = FileUploadUtils.getFileExt(name);
|
|
item.fileName = name;
|
|
string filename = FileUploadUtils.getFileName(name);
|
|
bool isValid = false;
|
|
if (filename.ToLower().EndsWith(".json"))
|
|
{
|
|
item.urlPath = remotePath + "/json/" + filename;
|
|
isValid = true;
|
|
}
|
|
else if (isMediaFile(filename))
|
|
{
|
|
item.urlPath = remotePath + "/media/" + filename;
|
|
isValid = true;
|
|
}
|
|
if (isValid)
|
|
{
|
|
item.time = FileUploadUtils.getFileCreateDataTime(name);
|
|
list.Add(item);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private void addFileToList(int type)
|
|
{
|
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
openFileDialog.Multiselect = true;
|
|
openFileDialog.Filter = "相机文件|*.mp4;*.avi;*.mkv;*.mov;*.wmv;*.flv;*.json|All Files|*.*";
|
|
openFileDialog.RestoreDirectory = true;
|
|
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (uploadFiles == null)
|
|
{
|
|
uploadFiles = new List<string>();
|
|
}
|
|
if (type == 0)
|
|
{
|
|
uploadFiles.Clear();
|
|
}
|
|
foreach (var fileName in openFileDialog.FileNames)
|
|
{
|
|
if (isValidFile(fileName))
|
|
{
|
|
uploadFiles.Add(fileName);
|
|
}
|
|
}
|
|
addToTable(uploadFiles);
|
|
changeButtonState(1);
|
|
}
|
|
}
|
|
private void btnSelFile_Click(object sender, EventArgs e)
|
|
{
|
|
addFileToList(0);
|
|
}
|
|
|
|
private void btnSysConfig_Click(object sender, EventArgs e)
|
|
{
|
|
pnlUpload.Visible = false;
|
|
pnlConfig.Dock = DockStyle.Fill;
|
|
pnlConfig.Visible = true;
|
|
}
|
|
|
|
private void btnOpenUpload_Click(object sender, EventArgs e)
|
|
{
|
|
pnlConfig.Visible = false;
|
|
pnlUpload.Dock = DockStyle.Fill;
|
|
pnlUpload.Visible = true;
|
|
}
|
|
|
|
private void loadConfigData()
|
|
{
|
|
string str = CommonUtils.readFromFile("common", "server_url");
|
|
txt_server_url.Text = str;
|
|
|
|
str = CommonUtils.readFromFile("ftp_server", "server_ip");
|
|
txt_ftp_server.Text = str;
|
|
|
|
str = CommonUtils.readFromFile("ftp_server", "server_port");
|
|
txt_server_port.Text = str;
|
|
|
|
str = CommonUtils.readFromFile("ftp_server", "ftp_user");
|
|
txt_ftp_user.Text = CommonUtils.DecryptToString(Convert.FromBase64String(str));
|
|
|
|
str = CommonUtils.readFromFile("ftp_server", "ftp_password");
|
|
txt_ftp_password.Text = CommonUtils.DecryptToString(Convert.FromBase64String(str));
|
|
|
|
}
|
|
|
|
private void btn_save_Click(object sender, EventArgs e)
|
|
{
|
|
string str = txt_server_url.Text;
|
|
if (str == "")
|
|
{
|
|
MessageBox.Show("WEB服务地址不能为空");
|
|
return;
|
|
}
|
|
CommonUtils.writeToIni("common", "server_url", str);
|
|
|
|
str = txt_ftp_server.Text;
|
|
if (str == "")
|
|
{
|
|
MessageBox.Show("文件服务地址不能为空");
|
|
return;
|
|
}
|
|
CommonUtils.writeToIni("ftp_server", "server_ip", str);
|
|
|
|
str = txt_server_port.Text;
|
|
if (str == "")
|
|
{
|
|
MessageBox.Show("文件服务端口不能为空");
|
|
return;
|
|
}
|
|
CommonUtils.writeToIni("ftp_server", "server_port", str);
|
|
|
|
str = txt_ftp_user.Text;
|
|
if (str == "")
|
|
{
|
|
MessageBox.Show("文件服务账号不能为空");
|
|
return;
|
|
}
|
|
str = Convert.ToBase64String(CommonUtils.EncryptString(str));
|
|
CommonUtils.writeToIni("ftp_server", "ftp_user", str);
|
|
|
|
str = txt_ftp_password.Text;
|
|
if (str == "")
|
|
{
|
|
MessageBox.Show("文件服务密码不能为空");
|
|
return;
|
|
}
|
|
str = Convert.ToBase64String(CommonUtils.EncryptString(str));
|
|
CommonUtils.writeToIni("ftp_server", "ftp_password", str);
|
|
MessageBox.Show("配置已保存");
|
|
|
|
}
|
|
|
|
private void addDirectoryToList(int type)
|
|
{
|
|
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
|
|
{
|
|
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
string selectedDirectoryPath = folderBrowserDialog.SelectedPath;
|
|
txtDirectory.Text = selectedDirectoryPath;
|
|
if (uploadFiles == null)
|
|
{
|
|
uploadFiles = new List<string>();
|
|
}
|
|
if (type == 0)
|
|
{
|
|
uploadFiles.Clear();
|
|
}
|
|
getAllMediaFile(selectedDirectoryPath, uploadFiles);
|
|
|
|
addToTable(uploadFiles);
|
|
|
|
if (uploadFiles.Count > 0)
|
|
{
|
|
changeButtonState(1);
|
|
}
|
|
else
|
|
{
|
|
//clearTable();
|
|
changeButtonState(2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnAddDirectory_Click(object sender, EventArgs e)
|
|
{
|
|
addDirectoryToList(1);
|
|
}
|
|
|
|
private void versionList_Validating(object sender, System.ComponentModel.CancelEventArgs e)
|
|
{
|
|
if (!versionList.Items.Contains(versionList.Text) && !string.IsNullOrEmpty(versionList.Text))
|
|
{
|
|
versionList.Items.Add(versionList.Text);
|
|
}
|
|
}
|
|
|
|
private void versionList_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (versionList.SelectedIndex == -1 && !versionList.Items.Contains(versionList.Text) && !string.IsNullOrEmpty(versionList.Text))
|
|
{
|
|
ComboboxItem versionItem = new ComboboxItem(versionList.Text, 0);
|
|
versionList.Items.Add(versionItem);
|
|
versionList.SelectedItem = versionItem;
|
|
}
|
|
}
|
|
|
|
private void btnAddfiles_Click(object sender, EventArgs e)
|
|
{
|
|
addFileToList(1);
|
|
}
|
|
}
|
|
|
|
|
|
}
|