| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System;
- using System.Collections;
- using System.IO;
- using System.IO.Compression;
- using System.Net;
- using System.Threading.Tasks;
- using Fort23.Core;
- using Fort23.UTool;
- using UnityEngine;
- using Utility;
- public class FTPManager : Singleton<FTPManager>
- {
- public string ftpServer = "ftp://139.155.99.185:21";
- public string ftpUser = "ck";
- public string ftpPass = "ck44526996"; // 或用私钥
- private string LocalLogPath => Path.Combine(Application.persistentDataPath, "GameLog.txt");
- private string LocalDataPath => Path.Combine(Application.persistentDataPath, "playerData.txt");
- private string ZipPath => Path.Combine(Application.persistentDataPath, "upload_logs.zip");
- [ContextMenu("asdada")]
- public async CTask<bool> UploadWithFTP()
- {
- try
- {
- CreateZipPackage();
- await UploadZipToFTPAsync();
- if (File.Exists(ZipPath)) File.Delete(ZipPath);
- Debug.Log("FTP 日志上传成功!");
- return true;
- }
- catch (System.Exception ex)
- {
- LogTool.Error($"上传失败: {ex.Message}\n{ex.StackTrace}");
- }
- return false;
- }
- private void CreateZipPackage()
- {
- if (File.Exists(ZipPath)) File.Delete(ZipPath);
- using (var zip = ZipFile.Open(ZipPath, ZipArchiveMode.Create))
- {
- if (File.Exists(LocalLogPath)) zip.CreateEntryFromFile(LocalLogPath, "GameLog.txt");
- if (File.Exists(LocalDataPath)) zip.CreateEntryFromFile(LocalDataPath, "playerData.txt");
- }
- }
- private async Task UploadZipToFTPAsync()
- {
- string fileName =
- $"logs_{DateTime.Now:yyyyMMdd_HHmmss}_{SystemInfo.deviceUniqueIdentifier}_{AccountFileInfo.Instance.playerData.playerId}.zip";
- string remoteUrl = new Uri(new Uri(ftpServer), fileName).ToString();
- FtpWebRequest request = (FtpWebRequest)WebRequest.Create(remoteUrl);
- request.Method = WebRequestMethods.Ftp.UploadFile;
- request.Credentials = new NetworkCredential(ftpUser, ftpPass);
- request.UseBinary = true;
- request.UsePassive = true;
- request.KeepAlive = false;
- request.Timeout = 30000;
- byte[] buffer = File.ReadAllBytes(ZipPath);
- request.ContentLength = buffer.Length;
- using (Stream requestStream = await request.GetRequestStreamAsync())
- {
- int offset = 0;
- int chunkSize = 64 * 1024; // 64KB
- while (offset < buffer.Length)
- {
- int size = Math.Min(chunkSize, buffer.Length - offset);
- await requestStream.WriteAsync(buffer, offset, size);
- offset += size;
- // 进度日志(主线程安全)
- float progress = (float)offset / buffer.Length;
- LogTool.Log($"[FTP] 上传中: {progress:P1}");
- }
- }
- using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
- {
- LogTool.Log($"[FTP] 服务器响应: {response.StatusDescription}");
- }
- }
- }
|