-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadAttachment.cs
161 lines (129 loc) · 6.02 KB
/
DownloadAttachment.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Rally.RestApi;
using Rally.RestApi.Response;
namespace DownloadAttachment
{
class Program
{
static void Main(string[] args)
{
RallyRestApi restApi;
String userName = "user@co.com";
String userPassword = "secret";
String rallyURL = "https://rally1.rallydev.com";
String wsapiVersion = "v2.0";
restApi = new RallyRestApi(
userName,
userPassword,
rallyURL,
wsapiVersion
);
String workspaceRef = "/workspace/12352608129";
String projectRef = "/project/12352608219";
bool projectScopingUp = false;
bool projectScopingDown = true;
Request storyRequest = new Request("hierarchicalrequirement");
storyRequest.Workspace = workspaceRef;
storyRequest.Project = projectRef;
storyRequest.ProjectScopeDown = projectScopingDown;
storyRequest.ProjectScopeUp = projectScopingUp;
storyRequest.Fetch = new List<string>()
{
"Name",
"FormattedID",
"Attachments"
};
storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US20");
QueryResult queryResult = restApi.Query(storyRequest);
DynamicJsonObject story = queryResult.Results.First();
// Grab the Attachments collection
Request attachmentsRequest = new Request(story["Attachments"]);
QueryResult attachmentsResult = restApi.Query(attachmentsRequest);
//Download the first attachment
var myAttachmentFromStory = attachmentsResult.Results.First();
String myAttachmentRef = myAttachmentFromStory["_ref"];
Console.WriteLine("Found Attachment: " + myAttachmentRef);
// Fetch fields for the Attachment
string[] attachmentFetch = { "ObjectID", "Name", "Content", "ContentType", "Size" };
// Now query for the attachment
DynamicJsonObject attachmentObject = restApi.GetByReference(myAttachmentRef, "true");
// Grab the AttachmentContent
DynamicJsonObject attachmentContentFromAttachment = attachmentObject["Content"];
String attachmentContentRef = attachmentContentFromAttachment["_ref"];
// Lastly pull the content
// Fetch fields for the Attachment
string[] attachmentContentFetch = { "ObjectID", "Content" };
// Now query for the attachment
Console.WriteLine("Querying for Content...");
DynamicJsonObject attachmentContentObject = restApi.GetByReference(attachmentContentRef, "true");
Console.WriteLine("AttachmentContent: " + attachmentObject["_ref"]);
String base64EncodedContent = attachmentContentObject["Content"];
// File information
String attachmentSavePath = "C:\\Users\\nmusaelian\\NewFolder";
String attachmentFileName = attachmentObject["Name"];
String fullAttachmentFile = attachmentSavePath + attachmentFileName;
// Determine attachment Content mime-type
String attachmentContentType = attachmentObject["ContentType"];
// Specify Image format
System.Drawing.Imaging.ImageFormat attachmentImageFormat;
try
{
attachmentImageFormat = getImageFormat(attachmentContentType);
}
catch (System.ArgumentException e)
{
Console.WriteLine("Invalid attachment file format:" + e.StackTrace);
}
try
{
// Convert base64 content to Image
Console.WriteLine("Converting base64 AttachmentContent String to Image.");
// Convert Base64 string to bytes
byte[] bytes = Convert.FromBase64String(base64EncodedContent);
Image myAttachmentImage;
using (MemoryStream ms = new MemoryStream(bytes))
{
myAttachmentImage = Image.FromStream(ms);
// Save the image
Console.WriteLine("Saving Image: " + fullAttachmentFile);
myAttachmentImage.Save(fullAttachmentFile, System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine("Finished Saving Attachment: " + fullAttachmentFile);
}
}
catch (Exception e)
{
Console.WriteLine("Unhandled exception occurred: " + e.StackTrace);
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
// Returns an ImageFormat type based on Rally contentType / mime-type
public static System.Drawing.Imaging.ImageFormat getImageFormat(String contentType)
{
// Save Image format
System.Drawing.Imaging.ImageFormat attachmentImageFormat;
switch (contentType)
{
case "image/png":
attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Png;
break;
case "image/jpeg":
attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "image/tiff":
attachmentImageFormat = System.Drawing.Imaging.ImageFormat.Tiff;
break;
default:
Console.WriteLine("Invalid image file format.");
throw new System.ArgumentException("Invalid attachment file format.");
};
return attachmentImageFormat;
}
}
}