Ejemplo n.º 1
0
        static async Task GetAndUploadFileToTeamsChannel(String aadAccessToken, String selectedTeamId, SemaphoreSlim semaphore, Combined.AttachmentsMapping combinedAttachmentsMapping, string channelSubFolder)
        {
            //string fileId = "";
            Tuple <string, string> fileIdAndUrl;

            try
            {
                string fileToUpload = "";
                if (!combinedAttachmentsMapping.attachmentUrl.StartsWith("/"))
                {
                    Console.WriteLine("Downloading attachment to local file system " + combinedAttachmentsMapping.attachmentId);
                    var request = new HttpClient();
                    using (HttpResponseMessage response =
                               await request.GetAsync(combinedAttachmentsMapping.attachmentUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
                    {
                        // do something with response
                        using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
                        {
                            fileToUpload = Path.GetTempFileName();
                            using (Stream streamToWriteTo = File.Open(fileToUpload, FileMode.Create))
                            {
                                await streamToReadFrom.CopyToAsync(streamToWriteTo);
                            }
                        }
                    }
                }
                else
                {
                    fileToUpload = combinedAttachmentsMapping.attachmentUrl;
                }
                var pathToItem = "/" + combinedAttachmentsMapping.msChannelName + "/slackArchive/fileattachments/" + combinedAttachmentsMapping.attachmentId + "/" + combinedAttachmentsMapping.attachmentFileName;
                fileIdAndUrl = await UploadFileToTeamsChannel(aadAccessToken, selectedTeamId, fileToUpload, pathToItem);

                combinedAttachmentsMapping.msSpoId  = fileIdAndUrl.Item1;
                combinedAttachmentsMapping.msSpoUrl = fileIdAndUrl.Item2;
                File.Delete(fileToUpload);
                Console.WriteLine("Deleting local copy of attachment " + combinedAttachmentsMapping.attachmentId);
            }
            catch (Exception ex)
            {
                // do something
                Console.WriteLine("Exception " + ex);
                Console.WriteLine("On this file " + combinedAttachmentsMapping.attachmentId + " " + combinedAttachmentsMapping.attachmentUrl);
            }
            finally
            {
                // don't forget to release
                semaphore.Release();
            }
            return;
        }
Ejemplo n.º 2
0
        static async Task GetAndUploadFileToTeamsChannel(String aadAccessToken, String selectedTeamId, SemaphoreSlim semaphore, Combined.AttachmentsMapping combinedAttachmentsMapping, string fileAttachmentsPath, AttachmentIdFilePathMode attachmentIdMode)
        {
            //string fileId = "";
            Tuple <string, string> fileIdAndUrl;

            try
            {
                var filename = combinedAttachmentsMapping.attachmentFileName;
                filename = filename.Replace(":", "-");
                switch (attachmentIdMode)
                {
                case AttachmentIdFilePathMode.Directory:
                    filename = combinedAttachmentsMapping.attachmentId + "/" + filename;
                    break;

                case AttachmentIdFilePathMode.Prefix:
                    filename = "(" + combinedAttachmentsMapping.attachmentId + ") " + filename;
                    break;

                case AttachmentIdFilePathMode.Suffix:
                    var extensionIndex = filename.LastIndexOf(".");
                    if (extensionIndex < 0)
                    {
                        filename = filename + " (" + combinedAttachmentsMapping.attachmentId + ")";
                    }
                    else
                    {
                        filename = filename.Substring(0, extensionIndex) + " (" + combinedAttachmentsMapping.attachmentId + ")" + filename.Substring(extensionIndex);
                    }
                    break;
                }
                var pathToItem = "/" + combinedAttachmentsMapping.msChannelName + fileAttachmentsPath + filename;

                bool   deleteFile   = true;
                string fileToUpload = "";
                if (combinedAttachmentsMapping.attachmentUrl.StartsWith("https") || combinedAttachmentsMapping.attachmentUrl.StartsWith("http"))
                {
                    Console.WriteLine("Downloading attachment to local file system " + combinedAttachmentsMapping.attachmentId);
                    var request = new HttpClient();
                    using (HttpResponseMessage response =
                               await request.GetAsync(combinedAttachmentsMapping.attachmentUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
                    {
                        // do something with response
                        using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
                        {
                            fileToUpload = Path.GetTempFileName();
                            using (Stream streamToWriteTo = File.Open(fileToUpload, FileMode.Create))
                            {
                                await streamToReadFrom.CopyToAsync(streamToWriteTo);
                            }
                        }
                    }
                }
                else
                {
                    fileToUpload = combinedAttachmentsMapping.attachmentUrl;
                    deleteFile   = false;
                }

                fileIdAndUrl = await UploadFileToTeamsChannel(aadAccessToken, selectedTeamId, fileToUpload, pathToItem, false);

                combinedAttachmentsMapping.msSpoId  = fileIdAndUrl.Item1;
                combinedAttachmentsMapping.msSpoUrl = fileIdAndUrl.Item2;
                if (deleteFile)
                {
                    File.Delete(fileToUpload);
                    Console.WriteLine("Deleting local copy of attachment " + combinedAttachmentsMapping.attachmentId);
                }
            }
            catch (Exception ex)
            {
                // do something
                Console.WriteLine("Exception " + ex);
                Console.WriteLine("On this file " + combinedAttachmentsMapping.attachmentId + " " + combinedAttachmentsMapping.attachmentUrl);
            }
            finally
            {
                // don't forget to release
                semaphore.Release();
            }
            return;
        }