public static bool UploadAttachment(string filename, string jiraId, string rawfile) { var config = (JiraConnectionConfiguration)ConfigurationManager.GetSection("jiraAttachments"); var metaconnector = new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/meta.v1/"); var dataconnector = new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/rest-1.v1/") .WithVersionOneUsernameAndPassword(config.V1Connection.Username, config.V1Connection.Password); var attachmentconnector = new VersionOneAPIConnector(config.V1Connection.ServerUrl + "/attachment.img/") .WithVersionOneUsernameAndPassword(config.V1Connection.Username, config.V1Connection.Password); MetaModel metaModel = new MetaModel(metaconnector); Services services = new Services(metaModel, dataconnector); Attachments attachments = new Attachments(attachmentconnector); string mimeType = MimeType.Resolve(filename); string assetoid = GetAssetOid(jiraId, "PrimaryWorkitem"); if (String.IsNullOrEmpty(assetoid)) { assetoid = GetAssetOid(jiraId, "Task"); } if (String.IsNullOrEmpty(assetoid)) return false; Oid attachmentContext = Oid.FromToken(assetoid, metaModel); IAssetType attachmentType = metaModel.GetAssetType("Attachment"); IAttributeDefinition attachmentNameDef = attachmentType.GetAttributeDefinition("Name"); IAttributeDefinition attachmentContentDef = attachmentType.GetAttributeDefinition("Content"); IAttributeDefinition attachmentContentTypeDef = attachmentType.GetAttributeDefinition("ContentType"); IAttributeDefinition attachmentFileNameDef = attachmentType.GetAttributeDefinition("Filename"); Asset newAttachment = services.New(attachmentType, attachmentContext); newAttachment.SetAttributeValue(attachmentNameDef, "Imported from Jira"); newAttachment.SetAttributeValue(attachmentContentDef, string.Empty); newAttachment.SetAttributeValue(attachmentContentTypeDef, mimeType); newAttachment.SetAttributeValue(attachmentFileNameDef, rawfile); services.Save(newAttachment); //Setup and attach the payload string attachmentKey = newAttachment.Oid.Key.ToString(); int buffersize = 4096; using (FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read)) { using (Stream output = attachments.GetWriteStream(attachmentKey)) { byte[] buffer = new byte[buffersize]; for (; ; ) { int read = input.Read(buffer, 0, buffersize); if (read == 0) break; output.Write(buffer, 0, read); } } } attachments.SetWriteStream(attachmentKey, mimeType); return true; }
private void UploadAttachmentContent(string AssetOID, byte[] FileContent, string FileType) { Attachments attachment = new Attachments(_imageConnector); //Writes the blob in one shot. using (Stream output = attachment.GetWriteStream(AssetOID)) { output.Write(FileContent, 0, FileContent.Length); } attachment.SetWriteStream(AssetOID, FileType); //Writes the blob in chunks. //int buffersize = 4096; //using (MemoryStream input = new MemoryStream(FileContent)) //{ // using (Stream output = attachment.GetWriteStream(AssetOID)) // { // byte[] buffer = new byte[input.Length + 1]; // for (;;) // { // int read = input.Read(buffer, 0, buffersize); // if (read <= 0) // break; // output.Write(buffer, 0, read); // } // } //} //attachment.SetWriteStream(AssetOID, FileType); }