Exemplo n.º 1
0
        public static bool FileSizeReached(int fileSize, IMG.FileType fileType, out string maxFileSize)
        {
            int maxFileSizeByType = (fileType == IMG.FileType.Image ? 10 : 200);

            maxFileSize = $"{maxFileSizeByType} MB";
            return(fileSize > maxFileSizeByType * 1000 * 1000);
        }
Exemplo n.º 2
0
        private async Task ProcessUpload(Message message)
        {
            ImgurModels.User ImgurUser = Utils.AddOrGetUser(message.From);

            long   chatId    = message.Chat.Id;
            int    messageId = message.MessageId;
            string fileId    = null;

            IMG.FileType fileType = IMG.FileType.Image;

            if (message.Type == TGENUMS.MessageType.Photo)
            {
                fileId = message.Photo.Last().FileId;
            }
            else if (message.Type == TGENUMS.MessageType.Document && Utils.IsMimeTypeImage(message.Document.MimeType)) /* Image send as doc */
            {
                fileId = message.Document.FileId;
            }
            else if (message.Type == TGENUMS.MessageType.Document && Utils.IsMimeTypeVideo(message.Document.MimeType)) /* GIF or Video */
            {
                fileId   = message.Document.FileId;
                fileType = IMG.FileType.Video;
            }

            if (string.IsNullOrEmpty(fileId))
            {
                await SendMessage(message.Chat.Id, "Can you send an Image?");

                return;
            }

            File tgFile = await Bot.GetFileAsync(fileId);

            if (Utils.FileSizeReached(tgFile.FileSize, fileType, out string maxFileSize))
            {
                await SendMessage(message.Chat.Id, $"Maximum file size reached! Max filesize for {fileType}: {maxFileSize}");

                return;
            }

            /* Handle File size */
            await UploadImage(ImgurUser.Id, chatId, messageId, tgFile, fileType);
        }
Exemplo n.º 3
0
        private async Task UploadImage(long imgUserId, long chatId, int messageId, File tgFile, IMG.FileType fileType)
        {
            Message reepplyMessage = await Bot.SendTextMessageAsync(chatId, "We are uploading your photo!", TGENUMS.ParseMode.Default, false, false, messageId);

            IImage imageUploaded = null;

            using (IO.MemoryStream fileStream = new IO.MemoryStream())
            {
                File imageFile = await Bot.GetInfoAndDownloadFileAsync(tgFile.FileId, fileStream);

                try
                {
                    fileStream.Position = 0;
                    imageUploaded       = await _imgurService.ImageEndpoint.UploadFileAsync(fileStream, imageFile.FilePath.Split('/').Last(), fileType, null, $"{imageFile.FileId} Upload by @imgurplusbot");
                }
                catch (Exception ex)
                {
                    Log.LogError("Exception throwed in UploadImage: {0} \n", ex.Message, ex.StackTrace);
                }
            }

            InlineKeyboardMarkup inlineKeyboardMarkup = null;

            if (imageUploaded != null)
            {
                List <InlineKeyboardButton[]> inlineKeyboardButtons = new List <InlineKeyboardButton[]>
                {
                    new InlineKeyboardButton[]     /* First Row */
                    {
                        InlineKeyboardButton.WithCallbackData("BB", new CallbackData(HandlerName, CallbackImageAction.ChangeUrl).AddData("urlType", "BB").ToString()),
                        InlineKeyboardButton.WithCallbackData("DELETE", new CallbackData(HandlerName, CallbackImageAction.Delete).AddData("deleteHash", imageUploaded.DeleteHash).ToString())
                    },
                    new InlineKeyboardButton[]     /* Second/Third Row */
                    {
                        InlineKeyboardButton.WithUrl("Open Link", imageUploaded.Link)
                    }
                };
                if (fileType == IMG.FileType.Video)
                {
                    inlineKeyboardButtons.Insert(1, new InlineKeyboardButton[] /* Second Row */
                    {
                        InlineKeyboardButton.WithCallbackData("MP4", new CallbackData(HandlerName, CallbackImageAction.ConvertToMP4).AddDataRange(new [] { ("H", imageUploaded.Id), ("DH", imageUploaded.DeleteHash) }).ToString()),