public override async Task Process(SchedulerJob job) { List <ulong> mediaIds = new List <ulong>(); foreach (var file in job.FilesToAttach) { byte[] mediaData = File.ReadAllBytes(file); if (mediaData.Length > TwitterConfig.MaxImageSize) { LogTo.Warn("Tried to attach image that was too big"); continue; } var usedAccounts = ContextList.Contexts.Where(c => job.AccountIds.Contains(c.UserId)).ToArray(); var acc = usedAccounts.First(); var additionalOwners = usedAccounts.Skip(1).Select(a => a.UserId); string mediaType = TwitterHelper.GetMimeType(file); var media = await acc.Twitter.UploadMediaAsync(mediaData, mediaType, additionalOwners); mediaIds.Add(media.MediaID); } var taskList = new List <Task>(); foreach (var accountId in job.AccountIds) { var context = ContextList.Contexts.FirstOrDefault(c => c.UserId == accountId); if (context == null) { LogTo.Warn($"Account with Id {accountId} was not found"); continue; } var task = context.Twitter.Statuses.TweetAsync(job.Text, mediaIds, job.InReplyToStatus); taskList.Add(task); } await Task.WhenAll(taskList); }
private async Task AttachImage(string fileName) { IsSending = true; if (Path.GetExtension(fileName) == ".gif") { if (GifValidator.Validate(fileName) != GifValidator.ValidationResult.Ok) { Notifier.DisplayMessage(Strings.ImageSizeTooBig, NotificationType.Error); IsSending = false; return; } } byte[] mediaData = File.ReadAllBytes(fileName); if (mediaData.Length > TwitterConfig.MaxImageSize) { Notifier.DisplayMessage(Strings.ImageSizeTooBig, NotificationType.Error); IsSending = false; return; } var usedAccounts = Accounts.Where(a => a.Use).ToArray(); var acc = usedAccounts.First(); var additionalOwners = usedAccounts.Skip(1).Select(a => a.Context.UserId); string mediaType = TwitterHelper.GetMimeType(fileName); var media = await acc.Context.Twitter.UploadMediaAsync(mediaData, mediaType, additionalOwners).ContinueWith(t => { IsSending = false; return(t.Result); }); await Dispatcher.RunAsync(() => { Medias.Add(media); AttachedMedias.Add(new MediaItem(media.MediaID, mediaData, fileName)); }); }
public void MimeTypeForFileIsDetectedCorrectly() { // Arrange var testCases = new Dictionary <string, string> { { "file.jpg", "image/jpg" }, { "file.png", "image/png" }, { "file.bmp", "image/bmp" }, { "file.gif", "image/gif" }, { "file.png.gif", "image/gif" }, { "file", "application/octet-stream" }, { "name.exe", "application/octet-stream" }, { "name", "application/octet-stream" } }; // Act var results = testCases.ToDictionary(kvp => kvp.Key, kvp => TwitterHelper.GetMimeType(kvp.Key)); // Assert foreach (var kvp in results) { Assert.AreEqual(testCases[kvp.Key], kvp.Value); } }
public bool CanExtract(string originalUrl) { var mime = TwitterHelper.GetMimeType(originalUrl); return(mime.StartsWith("image/")); }