forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetRestHandler.cs
More file actions
119 lines (108 loc) · 4.67 KB
/
Copy pathAssetRestHandler.cs
File metadata and controls
119 lines (108 loc) · 4.67 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.IO;
using System.Text;
using UnityEditorInternal;
using Object = UnityEngine.Object;
namespace UnityEditor.RestService
{
internal class AssetRestHandler
{
internal class AssetHandler : JSONHandler
{
protected override JSONValue HandleDelete(Request request, JSONValue payload)
{
var assetPath = request.Url.Substring("/unity/".Length);
var success = AssetDatabase.DeleteAsset(assetPath);
if (!success)
{
throw new RestRequestException
{
HttpStatusCode = HttpStatusCode.InternalServerError,
RestErrorString = "FailedDeletingAsset",
RestErrorDescription = "DeleteAsset() returned false"
};
}
return new JSONValue();
}
protected override JSONValue HandlePost(Request request, JSONValue payload)
{
var action = payload.Get("action").AsString();
switch (action)
{
case "move":
var oldFile = request.Url.Substring("/unity/".Length);
var newFile = payload.Get("newpath").AsString();
MoveAsset(oldFile, newFile);
break;
case "create":
var createPath = request.Url.Substring("/unity/".Length);
var contents = payload.Get("contents").AsString();
byte[] convertedBytes = Convert.FromBase64String(contents);
contents = Encoding.UTF8.GetString(convertedBytes);
CreateAsset(createPath, contents);
break;
default:
throw new RestRequestException {HttpStatusCode = HttpStatusCode.BadRequest, RestErrorString = "Uknown action: " + action};
}
return new JSONValue();
}
internal bool MoveAsset(string from, string to)
{
var result = AssetDatabase.MoveAsset(from, to);
if (result.Length > 0)
throw new RestRequestException(HttpStatusCode.BadRequest, "MoveAsset failed with error: " + result);
return result.Length == 0;
}
internal void CreateAsset(string assetPath, string contents)
{
var fullPath = Path.GetFullPath(assetPath);
try
{
using (StreamWriter writer = new StreamWriter(File.OpenWrite(fullPath)))
{
writer.Write(contents);
writer.Close();
}
}
catch (Exception e)
{
throw new RestRequestException(HttpStatusCode.BadRequest, "FailedCreatingAsset", "Caught exception: " + e);
}
}
protected override JSONValue HandleGet(Request request, JSONValue payload)
{
int splitIndex = request.Url.ToLowerInvariant().IndexOf("/assets/");
string assetPath = request.Url.ToLowerInvariant().Substring(splitIndex + 1);
return GetAssetText(assetPath);
}
internal JSONValue GetAssetText(string assetPath)
{
var theAsset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object));
if (theAsset == null)
throw new RestRequestException(HttpStatusCode.BadRequest, "AssetNotFound");
var result = new JSONValue();
result["file"] = assetPath;
result["contents"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(theAsset.ToString()));
return result;
}
}
internal class LibraryHandler : JSONHandler
{
//GET Requests, right now only "get all"
protected override JSONValue HandleGet(Request request, JSONValue payload)
{
var result = new JSONValue();
result["assets"] = ToJSON(AssetDatabase.FindAssets("", new[] {"Assets"}));
return result;
}
}
internal static void Register()
{
Router.RegisterHandler("/unity/assets", new LibraryHandler());
Router.RegisterHandler("/unity/assets/*", new AssetHandler());
}
}
}