-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Onchain and IPFS uploaders;
- Loading branch information
Showing
41 changed files
with
499 additions
and
232 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections; | ||
using TezosSDK.Scripts.FileUploaders.IPFS; | ||
using UnityEngine; | ||
|
||
namespace TezosSDK.Scripts.FileUploaders | ||
{ | ||
public abstract class BaseUploader : MonoBehaviour | ||
{ | ||
public string SupportedFileExtensions { get; } = ".jpg, .jpeg, .png"; | ||
|
||
private void Start() | ||
{ | ||
DontDestroyOnLoad(gameObject); | ||
} | ||
} | ||
|
||
public interface IPinataUploader : IBaseUploader | ||
{ | ||
PinataCredentials PinataCredentials { get; set; } | ||
} | ||
|
||
public interface IBaseUploader | ||
{ | ||
string SupportedFileExtensions { get; } | ||
|
||
/// <summary> | ||
/// Upload file that user will select through native menu file picker. | ||
/// </summary> | ||
/// <param name="callback"> | ||
/// Executes after asset uploaded with data address. | ||
/// </param> | ||
IEnumerator UploadFile(Action<string> callback); | ||
} | ||
} |
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace TezosSDK.Scripts.FileUploaders.IPFS | ||
{ | ||
public class PinataCredentials | ||
{ | ||
public string ApiUrl { get; } | ||
public string ApiKey { get; } | ||
|
||
public PinataCredentials(string apiKey) | ||
{ | ||
ApiUrl = "https://api.pinata.cloud/pinning/pinFileToIPFS"; | ||
ApiKey = apiKey; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
mergeInto(LibraryManager.library, { | ||
JsInitPinataUploader: function ( | ||
callbackObjectName, | ||
callbackMethodName, | ||
apiUrl, | ||
apiKey | ||
) { | ||
InitIpfsUploader({ | ||
CallbackObjectName: UTF8ToString(callbackObjectName), | ||
CallbackMethodName: UTF8ToString(callbackMethodName), | ||
ApiUrl: UTF8ToString(apiUrl), | ||
ApiKey: UTF8ToString(apiKey), | ||
}); | ||
}, | ||
|
||
JsInitBase64Uploader: function ( | ||
callbackObjectName, | ||
callbackMethodName | ||
) { | ||
InitBase64Uploader({ | ||
CallbackObjectName: UTF8ToString(callbackObjectName), | ||
CallbackMethodName: UTF8ToString(callbackMethodName) | ||
}); | ||
}, | ||
|
||
JsRequestUserFile: function (extensions) { | ||
FileUploader.RequestUserFile(UTF8ToString(extensions)); | ||
}, | ||
}); |
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
Runtime/Scripts/FileUploaders/OnChain/EditorBase64Uploader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#if UNITY_EDITOR | ||
using System; | ||
using System.Collections; | ||
using System.IO; | ||
using UnityEditor; | ||
|
||
|
||
namespace TezosSDK.Scripts.FileUploaders.OnChain | ||
{ | ||
public class EditorBase64Uploader : BaseUploader, IBaseUploader | ||
{ | ||
public IEnumerator UploadFile(Action<string> callback) | ||
{ | ||
yield return null; | ||
var imagePath = EditorUtility.OpenFilePanel( | ||
"Select image", | ||
string.Empty, | ||
SupportedFileExtensions | ||
.Replace(".", string.Empty) | ||
.Replace(" ", string.Empty) | ||
); | ||
callback.Invoke(ConvertImageToBase64(imagePath)); | ||
} | ||
|
||
private static string ConvertImageToBase64(string imagePath) | ||
{ | ||
var fileExtension = Path | ||
.GetExtension(imagePath) | ||
.Replace(".", string.Empty) | ||
.ToLower(); | ||
|
||
var imageBytes = File.ReadAllBytes(imagePath); | ||
var base64String = Convert.ToBase64String(imageBytes); | ||
return $"data:image/{fileExtension};base64,{base64String}"; | ||
} | ||
} | ||
} | ||
#endif |
3 changes: 3 additions & 0 deletions
3
Runtime/Scripts/FileUploaders/OnChain/EditorBase64Uploader.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
Runtime/Scripts/FileUploaders/OnChain/WebBase64Uploader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Runtime.InteropServices; | ||
using Logger = TezosSDK.Helpers.Logger; | ||
using UnityEngine; | ||
|
||
namespace TezosSDK.Scripts.FileUploaders.OnChain | ||
{ | ||
public class WebBase64Uploader : BaseUploader, IBaseUploader | ||
{ | ||
public void FileRequestCallback(string path) | ||
{ | ||
WebBase64UploaderHelper.SetResult(path); | ||
} | ||
|
||
public IEnumerator UploadFile(Action<string> callback) | ||
{ | ||
yield return null; | ||
WebBase64UploaderHelper.RequestFile(callback, SupportedFileExtensions); | ||
} | ||
} | ||
|
||
public static class WebBase64UploaderHelper | ||
{ | ||
private static Action<string> _responseCallback; | ||
|
||
public static IBaseUploader GetUploader() | ||
{ | ||
const string callbackObjectName = nameof(WebBase64Uploader); | ||
const string callbackMethodName = nameof(WebBase64Uploader.FileRequestCallback); | ||
|
||
var webUploaderGameObject = GameObject.Find(nameof(WebBase64Uploader)); | ||
var uploader = webUploaderGameObject != null | ||
? webUploaderGameObject.GetComponent<WebBase64Uploader>() | ||
: new GameObject(nameof(WebBase64Uploader)).AddComponent<WebBase64Uploader>(); | ||
|
||
JsInitBase64Uploader( | ||
callbackObjectName, | ||
callbackMethodName); | ||
|
||
return uploader; | ||
} | ||
|
||
public static void RequestFile(Action<string> callback, string extensions) | ||
{ | ||
JsRequestUserFile(extensions); | ||
_responseCallback = callback; | ||
} | ||
|
||
public static void SetResult(string response) | ||
{ | ||
_responseCallback.Invoke(response); | ||
Dispose(); | ||
} | ||
|
||
private static void Dispose() | ||
{ | ||
_responseCallback = null; | ||
} | ||
|
||
[DllImport("__Internal")] | ||
private static extern void JsInitBase64Uploader( | ||
string objectName, | ||
string methodName); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void JsRequestUserFile(string extensions); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Runtime/Scripts/FileUploaders/OnChain/WebBase64Uploader.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using TezosSDK.Scripts.FileUploaders.IPFS; | ||
using TezosSDK.Scripts.FileUploaders.OnChain; | ||
using UnityEngine; | ||
|
||
namespace TezosSDK.Scripts.FileUploaders | ||
{ | ||
public static class UploaderFactory | ||
{ | ||
/// <summary> | ||
/// Cross-platform image uploader to IPFS network via Pinata service. | ||
/// </summary> | ||
/// <param name="apiKey">API key from https://app.pinata.cloud/developers/api-keys</param> | ||
public static IBaseUploader GetPinataUploader(string apiKey) | ||
{ | ||
IPinataUploader uploader = null; | ||
|
||
#if UNITY_WEBGL && !UNITY_EDITOR | ||
uploader = WebPinataUploaderHelper.GetUploader(apiKey); | ||
#elif UNITY_EDITOR | ||
var editorUploaderGameObject = GameObject.Find(nameof(EditorPinataUploader)); | ||
uploader = editorUploaderGameObject != null | ||
? editorUploaderGameObject.GetComponent<EditorPinataUploader>() | ||
: new GameObject(nameof(EditorPinataUploader)).AddComponent<EditorPinataUploader>(); | ||
uploader.PinataCredentials = new PinataCredentials(apiKey); | ||
#endif | ||
return uploader; | ||
} | ||
|
||
public static IBaseUploader GetOnchainUploader() | ||
{ | ||
IBaseUploader uploader = null; | ||
|
||
#if UNITY_WEBGL && !UNITY_EDITOR | ||
uploader = WebBase64UploaderHelper.GetUploader(); | ||
#elif UNITY_EDITOR | ||
var editorUploaderGameObject = GameObject.Find(nameof(EditorBase64Uploader)); | ||
uploader = editorUploaderGameObject != null | ||
? editorUploaderGameObject.GetComponent<EditorBase64Uploader>() | ||
: new GameObject(nameof(EditorBase64Uploader)).AddComponent<EditorBase64Uploader>(); | ||
#endif | ||
return uploader; | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.