-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploadDemo.cs
executable file
·167 lines (144 loc) · 5.6 KB
/
UploadDemo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// UploadDemo.cs
// Creatubbles API Client Unity SDK
//
// Copyright (c) 2016 Creatubbles Pte. Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System;
using UnityEngine.Serialization;
using UnityEngine.UI;
using System.Collections.Generic;
using Creatubbles.Api;
using Creatubbles.Api.Storage;
using Creatubbles.Api.Requests;
using Creatubbles.Api.Data;
public class UploadDemo: MonoBehaviour
{
private const string StatusUploading = "Uploading...";
private const string StatusSuccess = "Success";
private const string StatusFailure = "Failed";
private const string StatusCancelled = "Cancelled";
public Image progressImage;
public InputField creationNameInput;
public InputField galleryIdInput;
public Button startButton;
public Button cancelButton;
public Text statusText;
public Image textureImage;
private CreatubblesApiClient creatubbles;
private CreationUploadSession creationUploadSession;
private string Status { set { statusText.text = value; } }
private Texture2D imageTexture { get { return textureImage.mainTexture as Texture2D; } }
// Use this for initialization.
void Start()
{
// configuration - in order for the API client to function properly it requires a simple configuration class containing information such as base URL, app ID and secret
// secureStorage - currently only InMemoryStorage is available as part of the SDK, however user can provide his / her own implementation
creatubbles = new CreatubblesApiClient(new CreatubblesConfiguration(), new InMemoryStorage());
}
// Update is called once per frame.
void Update()
{
if (creationUploadSession != null && !creationUploadSession.IsCancelled)
{
progressImage.fillAmount = creationUploadSession.UploadProgress;
}
}
public void StartUploadButtonClicked()
{
StartCoroutine(UploadCreation());
}
public void CancelUploadButtonClicked()
{
if (creationUploadSession != null)
{
creationUploadSession.Cancel();
}
}
// Creates a new Creation entity and uploads an image with it.
IEnumerator UploadCreation()
{
Started();
NewCreationData creationData;
byte[] imageData = imageTexture.EncodeToPNG();
creationData = new NewCreationData(imageData, UploadExtension.PNG);
creationData.name = creationNameInput.text;
creationData.galleryId = galleryIdInput.text;
creationData.creationMonth = DateTime.Now.Month;
creationData.creationYear = DateTime.Now.Year;
creationUploadSession = new CreationUploadSession(creationData);
yield return creationUploadSession.Upload(creatubbles);
// cancelling upload session will usually cause a System or Internal error to be reported, so we should always check for cancellation before checking for errors
if (creationUploadSession.IsCancelled)
{
Cancelled();
yield break;
}
if (creationUploadSession.IsAnyError)
{
if (creationUploadSession.IsSystemError)
{
Debug.Log("System error: " + creationUploadSession.SystemError);
}
else if (creationUploadSession.IsApiError && creationUploadSession.ApiErrors != null && creationUploadSession.ApiErrors.Length > 0)
{
// if unauthorized, log user out
Debug.Log("API error: " + creationUploadSession.ApiErrors[0].title);
}
else if (creationUploadSession.IsInternalError)
{
Debug.Log("Internal error: " + creationUploadSession.InternalError);
}
creationUploadSession = null;
Failed();
yield break;
}
creationUploadSession = null;
Successful();
}
private void Started()
{
Status = StatusUploading;
progressImage.fillAmount = 0;
startButton.interactable = false;
cancelButton.interactable = true;
}
private void Successful()
{
Status = StatusSuccess;
startButton.interactable = true;
cancelButton.interactable = false;
}
private void Failed()
{
Status = StatusFailure;
startButton.interactable = true;
cancelButton.interactable = false;
}
private void Cancelled()
{
Status = StatusCancelled;
startButton.interactable = true;
cancelButton.interactable = false;
}
}