Example #1
0
 public void AttachFileToPage(long pageId, string title, string fileName, string filePath, string fileContentType, string attachedFileName = null)
 {
     this.ExecuteLoggedIn(
         (confluence, token) =>
     {
         var file = new FileInfo(Path.Combine(filePath, fileName));
         using (var fileStream = file.OpenRead())
             using (var reader = new StreamReader(fileStream, Encoding.UTF8))
             {
                 var str            = reader.ReadToEnd();
                 var attachmentData = new byte[str.Length * sizeof(char)];
                 Buffer.BlockCopy(str.ToCharArray(), 0, attachmentData, 0, attachmentData.Length);
                 if (attachmentData.Length > 0)
                 {
                     var attachment = new RemoteAttachment
                     {
                         pageId      = pageId,
                         title       = title,
                         fileName    = attachedFileName ?? fileName,
                         contentType = fileContentType,
                         created     = DateTime.Now,
                         creator     = this.UserName,
                         fileSize    = attachmentData.Length
                     };
                     confluence.addAttachment(token, attachment, attachmentData);
                 }
             }
     });
 }
            public void IfIssueIsCreated_ShouldLoadAttachments()
            {
                //arrange
                var jira             = TestableJira.Create();
                var webClient        = new Mock <IWebClient>();
                var remoteAttachment = new RemoteAttachment()
                {
                    filename = "attach.txt"
                };

                jira.IssueService.Setup(j => j.GetAttachmentsAsync("issueKey", It.IsAny <CancellationToken>()))
                .Returns(Task.FromResult(Enumerable.Repeat <Attachment>(new Attachment(jira, webClient.Object, remoteAttachment), 1)));

                var issue = (new RemoteIssue()
                {
                    key = "issueKey"
                }).ToLocal(jira);

                //act
                var attachments = issue.GetAttachmentsAsync().Result;

                //assert
                Assert.Single(attachments);
                Assert.Equal("attach.txt", attachments.First().FileName);
            }
        public void addAttachment(long pageId, string mime, string comment, string filename, IBinaryContainer image)
        {
            checkCredentials();
            RemoteAttachment attachment = new RemoteAttachment();

            // Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395
            attachment.comment     = comment;
            attachment.fileName    = filename;
            attachment.contentType = mime;
            confluence.addAttachment(credentials, pageId, attachment, image.ToByteArray());
        }
 /// <summary>
 /// Creates a new instance of an Attachment from a remote entity.
 /// </summary>
 /// <param name="jira">Object used to interact with JIRA.</param>
 /// <param name="webClient">WebClient to use to download attachment.</param>
 /// <param name="remoteAttachment">Remote attachment entity.</param>
 public Attachment(Jira jira, IWebClient webClient, RemoteAttachment remoteAttachment)
 {
     _jira      = jira;
     _author    = remoteAttachment.author;
     _created   = remoteAttachment.created;
     _fileName  = remoteAttachment.filename;
     _mimeType  = remoteAttachment.mimetype;
     _fileSize  = remoteAttachment.filesize;
     _id        = remoteAttachment.id;
     _webClient = webClient;
 }
        /// <summary>
        /// Creates a new instance of an Attachment from a remote entity.
        /// </summary>
        /// <param name="jira">Object used to interact with JIRA.</param>
        /// <param name="remoteAttachment">Remote attachment entity.</param>
        public Attachment(Jira jira, RemoteAttachment remoteAttachment)
        {
            _jira = jira;

            AuthorUser  = remoteAttachment.authorUser;
            CreatedDate = remoteAttachment.created;
            FileName    = remoteAttachment.filename;
            MimeType    = remoteAttachment.mimetype;
            FileSize    = remoteAttachment.filesize;
            Id          = remoteAttachment.id;
        }
Example #6
0
 public Attachment(RemoteAttachment attachment)
 {
     Comment     = attachment.comment;
     ContentType = attachment.contentType;
     DateTime    = attachment.created;
     Creator     = attachment.creator;
     FileName    = attachment.fileName;
     FileSize    = attachment.fileSize;
     Id          = attachment.id;
     PageId      = attachment.pageId;
     Title       = attachment.title;
     Url         = attachment.url;
 }
Example #7
0
        protected override void ProcessRecord()
        {
            // get filename from FilePath if not already set
            FileName = !string.IsNullOrEmpty(FileName) ? FileName : new FileInfo(FilePath).Name;

            var attachment = new RemoteAttachment
            {
                comment     = Comment,
                contentType = ContentType,
                fileName    = FileName
            };

            var resultAtt = Service.AddAttachment(attachment, PageId, File.ReadAllBytes(FilePath));

            WriteObject(new Attachment(resultAtt));
        }
Example #8
0
        public void LogFileConversion(XDoc spaceManifest, RemoteAttachment fileInfo, string contentUrl)
        {
            string xpath = string.Format("file[@c.fileid='{0}']", fileInfo.id);

            XDoc fileXml = spaceManifest[xpath];

            if (fileXml.IsEmpty)
            {
                spaceManifest.Start("file");
                spaceManifest.Attr("c.fileid", fileInfo.id);
                spaceManifest.Attr("c.pageid", fileInfo.pageId);
                spaceManifest.Attr("c.filename", fileInfo.fileName);
                spaceManifest.Attr("c.filesize", fileInfo.fileSize);
                spaceManifest.Attr("c.mimetype", fileInfo.contentType);
                spaceManifest.Attr("c.path", Utils.GetUrlLocalUri(_confBaseUrl, fileInfo.url, false, true));
                spaceManifest.Attr("mt.path", Utils.GetApiUrl(Utils.GetUrlLocalUri(_confBaseUrl, contentUrl, true, true)));
                spaceManifest.End();
            }
        }
Example #9
0
        public RemoteAttachment AddAttachment(string fileName, string title, string parent, string space, string comment)
        {
            try
            {
                byte[]           data       = null;
                RemoteAttachment attachment = new RemoteAttachment();
                data = File.ReadAllBytes(fileName);
                attachment.fileName    = Path.GetFileName(fileName);
                attachment.contentType = "application/zip";
                attachment.comment     = comment;
                attachment.created     = DateTime.Now;

                RemotePage page = GetPage(title, parent, space);
                attachment = m_soapService.addAttachment(m_soapAuthToken, page.id, attachment, data);

                return(attachment);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Error adding attachment: " + ex.Message + Environment.NewLine + "Location: " + ex.StackTrace);
            }
            return(null);
        }
Example #10
0
 public RemoteAttachment AddAttachment(RemoteAttachment attachment, long pageId, byte[] fileBytes)
 {
     return(_service.addAttachment(_authToken, pageId, attachment, fileBytes));
 }
Example #11
0
 /// <summary>
 /// Create a new Attachment from a RemoteAttachment
 /// </summary>
 public static Attachment ToLocal(this RemoteAttachment remoteAttachment, Jira jira, IWebClient webClient)
 {
     return(new Attachment(jira, webClient, remoteAttachment));
 }
        public IList <SearchIndexDocument> GetContentToIndex()
        {
            var searchDocuments    = new List <SearchIndexDocument>();
            var searchConfig       = Telligent.Common.Services.Get <SearchConfiguration>();
            var maxFileSizeInBytes = searchConfig.MaxAttachmentFileSizeMB * 1024 * 1024;
            var credentialsManager = ServiceLocator.Get <ICredentialsManager>();
            var items = listItemService.ListItemsToReindex(LibraryApplicationType.Id, 500);

            foreach (var item in items)
            {
                try
                {
                    var document = new Document(item);
                    if (document.IsFolder)
                    {
                        continue;
                    }

                    var extMatch = XExt.Match(document.Name);
                    var ext      = extMatch.Success ? extMatch.Groups[0].Value.TrimStart('.') : string.Empty;

                    var doc = TEApi.SearchIndexing.NewDocument(
                        document.ContentId,
                        Id,
                        "Document",
                        PublicApi.SharePointUrls.Document(document.ContentId),
                        document.DisplayName,
                        PublicApi.Documents.Events.OnRender(document, "Description", document.Name, "unknown"));


                    doc.AddField(TEApi.SearchIndexing.Constants.RelatedId, document.ContentId.ToString());
                    doc.AddField(TEApi.SearchIndexing.Constants.IsContent, true.ToString());
                    doc.AddField(TEApi.SearchIndexing.Constants.ContentID, document.ContentId.ToString());
                    doc.AddField(TEApi.SearchIndexing.Constants.ApplicationId, document.Library.Id.ToString());
                    doc.AddField(TEApi.SearchIndexing.Constants.GroupID, document.Library.GroupId.ToString(CultureInfo.InvariantCulture));
                    doc.AddField(TEApi.SearchIndexing.Constants.ContainerId, document.Library.Container.ContainerId.ToString());

                    doc.AddField("sp_fileextension", ext.ToLowerInvariant());
                    doc.AddField(TEApi.SearchIndexing.Constants.CollapseField, string.Format("document:{0}", document.ContentId));
                    doc.AddField(TEApi.SearchIndexing.Constants.Date, TEApi.SearchIndexing.FormatDate(document.CreatedDate));
                    doc.AddField(TEApi.SearchIndexing.Constants.Category, "Documents");

                    var user = TEApi.Users.Get(new UsersGetOptions {
                        Email = document.Author.Email
                    });
                    if (user != null && !user.HasErrors())
                    {
                        doc.AddField(TEApi.SearchIndexing.Constants.UserDisplayName, user.DisplayName);
                        doc.AddField(TEApi.SearchIndexing.Constants.Username, user.Username);
                        doc.AddField(TEApi.SearchIndexing.Constants.CreatedBy, user.DisplayName);
                    }

                    var tags = TEApi.Tags.Get(document.ContentId, Id, null);
                    if (tags != null)
                    {
                        foreach (var tag in tags)
                        {
                            doc.AddField(TEApi.SearchIndexing.Constants.TagKeyword, tag.TagName.ToLower());
                            doc.AddField(TEApi.SearchIndexing.Constants.Tag, tag.TagName);
                        }
                    }

                    doc.AddField(TEApi.SearchIndexing.Constants.AttachmentName, document.Name);
                    doc.AddField(TEApi.SearchIndexing.Constants.AttachmentText, RemoteAttachment.GetText(document.Library.SPWebUrl, document.Name, document.Path, credentialsManager, maxFileSizeInBytes));

                    foreach (var field in item.EditableFields())
                    {
                        var key   = string.Format("sp_{0}", field.InternalName.ToLowerInvariant());
                        var value = item.ValueAsText(field.InternalName);
                        if (value != null)
                        {
                            doc.AddField(key, value.ToLowerInvariant());
                        }
                    }

                    searchDocuments.Add(doc);
                }
                catch { }
            }

            return(searchDocuments);
        }
Example #13
0
 /// <summary>
 /// Create a new Attachment from a RemoteAttachment
 /// </summary>
 public static Attachment ToLocal(this RemoteAttachment remoteAttachment, Jira jira)
 {
     return(new Attachment(jira, remoteAttachment));
 }
        public IList <SearchIndexDocument> GetContentToIndex()
        {
            var searchDocuments    = new List <SearchIndexDocument>();
            var searchConfig       = Telligent.Common.Services.Get <SearchConfiguration>();
            var maxFileSizeInBytes = searchConfig.MaxAttachmentFileSizeMB * 1024 * 1024;
            var credentialsManager = ServiceLocator.Get <ICredentialsManager>();
            var items = listItemService.ListItemsToReindex(ListApplicationType.Id, 500);

            foreach (var item in items)
            {
                var doc = TEApi.SearchIndexing.NewDocument(
                    item.ContentId,
                    Id,
                    "ListItem",
                    PublicApi.SharePointUrls.ListItem(item.ContentId),
                    item.DisplayName,
                    PublicApi.ListItems.Events.OnRender(item, "Description", item.DisplayName, "unknown"));

                var list = PublicApi.Lists.Get(item.ListId);

                doc.AddField(TEApi.SearchIndexing.Constants.RelatedId, item.ContentId.ToString());
                doc.AddField(TEApi.SearchIndexing.Constants.IsContent, true.ToString());
                doc.AddField(TEApi.SearchIndexing.Constants.ContentID, item.ContentId.ToString());
                doc.AddField(TEApi.SearchIndexing.Constants.ApplicationId, list.Id.ToString());
                doc.AddField(TEApi.SearchIndexing.Constants.GroupID, list.GroupId.ToString(CultureInfo.InvariantCulture));
                doc.AddField(TEApi.SearchIndexing.Constants.ContainerId, list.Container.ContainerId.ToString());

                doc.AddField(TEApi.SearchIndexing.Constants.CollapseField, string.Format("listitem:{0}", item.ContentId));
                doc.AddField(TEApi.SearchIndexing.Constants.Date, TEApi.SearchIndexing.FormatDate(item.CreatedDate));
                doc.AddField(TEApi.SearchIndexing.Constants.Category, "ListItems");

                var user = TEApi.Users.Get(new UsersGetOptions {
                    Email = item.Author.Email
                });
                if (user != null && !user.HasErrors())
                {
                    doc.AddField(TEApi.SearchIndexing.Constants.UserDisplayName, user.DisplayName);
                    doc.AddField(TEApi.SearchIndexing.Constants.Username, user.Username);
                    doc.AddField(TEApi.SearchIndexing.Constants.CreatedBy, user.DisplayName);
                }

                var tags = TEApi.Tags.Get(item.ContentId, Id, null);
                if (tags != null)
                {
                    foreach (var tag in tags)
                    {
                        doc.AddField(TEApi.SearchIndexing.Constants.TagKeyword, tag.TagName.ToLower());
                        doc.AddField(TEApi.SearchIndexing.Constants.Tag, tag.TagName);
                    }
                }

                foreach (var field in item.EditableFields())
                {
                    if (field.FieldTypeKind == Microsoft.SharePoint.Client.FieldType.Attachments)
                    {
                        var webUrl     = list.SPWebUrl.ToLowerInvariant();
                        var atachments = PublicApi.Attachments.List(item.ListId, new AttachmentsGetOptions(item.ContentId, field.InternalName));
                        foreach (var attachment in atachments)
                        {
                            string attachmentUrl = attachment.Uri.ToString().ToLowerInvariant();
                            if (attachmentUrl.StartsWith(webUrl))
                            {
                                string path = attachmentUrl.Replace(webUrl, string.Empty);
                                doc.AddField(string.Format("sp_{0}_{1}_name", field.InternalName.ToLowerInvariant(), attachment.Name.ToLowerInvariant()), attachment.Name);
                                doc.AddField(string.Format("sp_{0}_{1}_text", field.InternalName.ToLowerInvariant(), attachment.Name.ToLowerInvariant()), RemoteAttachment.GetText(webUrl, attachment.Name, path, credentialsManager, maxFileSizeInBytes));
                            }
                        }
                    }
                    else
                    {
                        var key   = string.Format("sp_{0}", field.InternalName.ToLowerInvariant());
                        var value = item.ValueAsText(field.InternalName);
                        if (value != null)
                        {
                            doc.AddField(key, value.ToLowerInvariant());
                        }
                    }
                }

                searchDocuments.Add(doc);
            }

            return(searchDocuments);
        }
Example #15
0
 /// <summary>
 /// Creates a new instance of an Attachment from a remote entity.
 /// </summary>
 /// <param name="jira">Object used to interact with JIRA.</param>
 /// <param name="webClient">WebClient to use to download attachment.</param>
 /// <param name="remoteAttachment">Remote attachment entity.</param>
 public Attachment(Jira jira, IWebClient webClient, RemoteAttachment remoteAttachment) :
     this(jira.Url, webClient, remoteAttachment)
 {
 }