public static async Task UploadImages(string screenName, string [] files, string message, bool fromGroup, VkNet.Enums.VkObjectType type, long ownerId) { string result = "OK"; try { var serverUp = Api.Photo.GetWallUploadServer(ownerId); var url = serverUp.UploadUrl; var coll = new List<Photo>().AsEnumerable(); int cnt = 0, total = files.Length; if (OnTotalProgressChange != null) OnTotalProgressChange(cnt, total); foreach (var file in files) { var res = await UploadFile(url, file); var response = JsonConvert.DeserializeObject<PhotoResponse>(res); var col = Api.Photo.SaveWallPhoto(response.Photo, Api.UserId, ownerId, response.Server, response.Hash); coll = coll.Concat(col); cnt++; if (OnTotalProgressChange != null) OnTotalProgressChange(cnt, total); } try { var post = Api.Wall.Post(ownerId * (type == VkNet.Enums.VkObjectType.Group ? -1 : 1), false, fromGroup, message, coll, signed: true); } catch { if(fromGroup) Api.Wall.Post(ownerId * (type == VkNet.Enums.VkObjectType.Group ? -1 : 1), false, !fromGroup, message, coll, signed: true); } } catch (AccessDeniedException e) { result = "Доступ запрещен!"; } catch (Exception e) { if(result == "OK") result = "Не удалось загрузить..." + e.Message; } Dispatcher.CurrentDispatcher.Invoke(() => { MessageBox.Show(result == "OK" ? "Фотографии успешно загружены" : result); }); }
// TODO change this method to more understandable public long GetSize(VkNet.Model.Attachments.Audio audio) { if (cacheDictionary.ContainsKey(audio.Id)) return cacheDictionary[audio.Id]; try { //from http://stackoverflow.com/questions/122853/get-http-file-size System.Net.WebRequest req = System.Net.HttpWebRequest.Create(audio.Url); req.Method = "HEAD"; using (System.Net.WebResponse resp = req.GetResponse()) { long length; if (long.TryParse(resp.Headers.Get("Content-Length"), out length)) { cacheDictionary[audio.Id] = length; Save(); return length; } return 0; } } catch (Exception) { return 0; } }