You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
4.1 KiB
134 lines
4.1 KiB
using System;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using System.IO;
|
|
using System.Net;
|
|
using ZXing;
|
|
using ZXing.QrCode;
|
|
using System.Threading;
|
|
using System.Text.RegularExpressions;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.Events;
|
|
|
|
namespace UC
|
|
{
|
|
public class FTP : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private string host = "ftp://ultracombos.com/";
|
|
[SerializeField]
|
|
private string httpAddress = "https://www.ultracombos.com/techtest/";
|
|
[SerializeField]
|
|
private string user = "techtest";
|
|
[SerializeField]
|
|
private string pass = "UcRocks1!";
|
|
|
|
[Range(1, 6)]
|
|
public int uploadTexDevideRate = 1;
|
|
|
|
struct FTPData
|
|
{
|
|
public string filename;
|
|
public byte[] data;
|
|
}
|
|
|
|
public struct FTPUploadResult
|
|
{
|
|
public bool success;
|
|
public string publicUrl;
|
|
public FTPUploadResult(string publicUrl, bool success)
|
|
{
|
|
this.success = success;
|
|
this.publicUrl = publicUrl;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class FTPUploadEvent: UnityEvent<FTPUploadResult> { };
|
|
[SerializeField]
|
|
public FTPUploadEvent uploadEvent = new FTPUploadEvent();
|
|
|
|
List<FTPUploadResult> results = new List<FTPUploadResult>();
|
|
|
|
private void Update()
|
|
{
|
|
lock(results)
|
|
{
|
|
foreach(var r in results)
|
|
{
|
|
if(uploadEvent != null)
|
|
{
|
|
uploadEvent.Invoke(r);
|
|
}
|
|
}
|
|
results.Clear();
|
|
}
|
|
}
|
|
|
|
public void Upload(Texture tex)
|
|
{
|
|
Texture2D newTex = UC.TextureUtil.Resize(tex, uploadTexDevideRate);
|
|
Debug.Log("upload tex size: " + newTex.width + " " + newTex.height);
|
|
byte[] pngBytes = newTex.EncodeToPNG();
|
|
|
|
string datetime = DateTime.Now.ToString("yyyyMMddHHmmssss");
|
|
string hash = Regex.Replace(DateTime.Now.GetHashCode().ToString(), @"-", "");
|
|
string filename = "img_" + datetime + "_" + hash + ".png";
|
|
|
|
FTPData data = new FTPData();
|
|
data.filename = filename;
|
|
data.data = pngBytes;
|
|
|
|
Thread loadingThread = new Thread(new ParameterizedThreadStart(UploadThread));
|
|
loadingThread.Start(data);
|
|
Destroy(newTex);
|
|
}
|
|
|
|
void UploadThread(object obj)
|
|
{
|
|
FTPData data = (FTPData)obj;
|
|
Stream ftpStream = null;
|
|
Debug.Log("Upload begin");
|
|
try
|
|
{
|
|
//private FtpWebResponse ftpResponse = null;
|
|
string uri = host + "/" + data.filename;
|
|
Debug.Log(uri);
|
|
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
ftpRequest.Credentials = new NetworkCredential(user, pass);
|
|
ftpRequest.UseBinary = true;
|
|
ftpRequest.UsePassive = true;
|
|
ftpRequest.KeepAlive = true;
|
|
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
|
|
ftpStream = ftpRequest.GetRequestStream();
|
|
ftpStream.Write(data.data, 0, data.data.Length);
|
|
|
|
lock(results)
|
|
{
|
|
string url = httpAddress + data.filename;
|
|
Debug.Log("Upload success to url: " + url);
|
|
|
|
FTPUploadResult r = new FTPUploadResult(url, true);
|
|
results.Add(r);
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.Log("Upload fail!!!");
|
|
print(e);
|
|
if (uploadEvent != null)
|
|
{
|
|
FTPUploadResult r = new FTPUploadResult("", false);
|
|
results.Add(r);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ftpStream.Close();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|