-
Notifications
You must be signed in to change notification settings - Fork 86
/
OSSController.cs
165 lines (147 loc) · 5.86 KB
/
OSSController.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
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
using Autodesk.Forge;
using Autodesk.Forge.Model;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace forgesample.Controllers
{
public class OSSController : ApiController
{
public string ClientId { get { return OAuthController.GetAppSetting("FORGE_CLIENT_ID").ToLower(); } }
/// <summary>
/// Return list of buckets (id=#) or list of objects (id=bucketKey)
/// </summary>
[HttpGet]
[Route("api/forge/oss/buckets")]
public async Task<IList<TreeNode>> GetOSSAsync([FromUri]string id)
{
IList<TreeNode> nodes = new List<TreeNode>();
dynamic oauth = await OAuthController.GetInternalAsync();
if (id == "#") // root
{
// in this case, let's return all buckets
BucketsApi appBckets = new BucketsApi();
appBckets.Configuration.AccessToken = oauth.access_token;
// to simplify, let's return only the first 100 buckets
dynamic buckets = await appBckets.GetBucketsAsync("US", 100);
foreach (KeyValuePair<string, dynamic> bucket in new DynamicDictionaryItems(buckets.items))
{
nodes.Add(new TreeNode(bucket.Value.bucketKey, bucket.Value.bucketKey.Replace(ClientId + "-", string.Empty), "bucket", true));
}
}
else
{
// as we have the id (bucketKey), let's return all
ObjectsApi objects = new ObjectsApi();
objects.Configuration.AccessToken = oauth.access_token;
var objectsList = objects.GetObjects(id);
foreach (KeyValuePair<string, dynamic> objInfo in new DynamicDictionaryItems(objectsList.items))
{
nodes.Add(new TreeNode(Base64Encode((string)objInfo.Value.objectId),
objInfo.Value.objectKey, "object", false));
}
}
return nodes;
}
/// <summary>
/// Model data for jsTree used on GetOSSAsync
/// </summary>
public class TreeNode
{
public TreeNode(string id, string text, string type, bool children)
{
this.id = id;
this.text = text;
this.type = type;
this.children = children;
}
public string id { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool children { get; set; }
}
/// <summary>
/// Create a new bucket
/// </summary>
[HttpPost]
[Route("api/forge/oss/buckets")]
public async Task<dynamic> CreateBucket([FromBody]CreateBucketModel bucket)
{
BucketsApi buckets = new BucketsApi();
dynamic token = await OAuthController.GetInternalAsync();
buckets.Configuration.AccessToken = token.access_token;
PostBucketsPayload bucketPayload = new PostBucketsPayload(string.Format("{0}-{1}", ClientId, bucket.bucketKey.ToLower()), null,
PostBucketsPayload.PolicyKeyEnum.Transient);
return await buckets.CreateBucketAsync(bucketPayload, "US");
}
/// <summary>
/// Input model for CreateBucket method
/// </summary>
public class CreateBucketModel
{
public string bucketKey { get; set; }
}
/// <summary>
/// Receive a file from the client and upload to the bucket
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("api/forge/oss/objects")]
public async Task<dynamic> UploadObject()
{
// basic input validation
HttpRequest req = HttpContext.Current.Request;
if (string.IsNullOrWhiteSpace(req.Params["bucketKey"]))
throw new System.Exception("BucketKey parameter was not provided.");
if (req.Files.Count != 1)
throw new System.Exception("Missing file to upload"); // for now, let's support just 1 file at a time
string bucketKey = req.Params["bucketKey"];
HttpPostedFile file = req.Files[0];
// save the file on the server
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), file.FileName);
file.SaveAs(fileSavePath);
// get the bucket...
dynamic oauth = await OAuthController.GetInternalAsync();
ObjectsApi objects = new ObjectsApi();
objects.Configuration.AccessToken = oauth.access_token;
// upload the file/object, which will create a new object
dynamic uploadedObj;
using (StreamReader streamReader = new StreamReader(fileSavePath))
{
uploadedObj = await objects.UploadObjectAsync(bucketKey,
file.FileName, (int)streamReader.BaseStream.Length, streamReader.BaseStream,
"application/octet-stream");
}
// cleanup
File.Delete(fileSavePath);
return uploadedObj;
}
/// <summary>
/// Base64 enconde a string
/// </summary>
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
}
}