Пример #1
0
        public bool DownloadHomeworkFile(HomeworkEntity homework, string downloadPath = null)
        {
            try
            {
                Logger.Log("Downloading homework file...", ConsoleColor.Yellow);
                DirectoryInfo info = null;
                if (downloadPath != null && Directory.Exists(downloadPath))
                {
                    info = new DirectoryInfo(downloadPath);
                }
                else
                {
                    info = new DirectoryInfo(Environment.CurrentDirectory);
                }

                using (var client = new WebClient())
                {
                    client.DownloadFile(homework.Filepath, info.FullName + $"\\{homework.Filename}");
                }
                Logger.Log($"Downloading done. Path: {info.FullName}\\{homework.Filename}", ConsoleColor.Green);
                return(true);
            }
            catch (Exception e)
            {
                Logger.Log(e.Message, ConsoleColor.Red);
                return(false);
            }
        }
Пример #2
0
        public bool UploadHomeworkFile(HomeworkEntity homework, string pathToFile)
        {
            try
            {
                Logger.Log("Uploading file...", ConsoleColor.Yellow);

                HttpClient httpClient         = new HttpClient();
                MultipartFormDataContent form = new MultipartFormDataContent();

                form.Add(new StringContent(AccessToken), "token");
                form.Add(new StringContent("prod"), "env");
                form.Add(new StringContent("create"), "action");
                byte[] data = File.ReadAllBytes(pathToFile);
                form.Add(new ByteArrayContent(data, 0, data.Length), "file", Path.GetFileName(pathToFile));
                form.Add(new StringContent("1"), "type");

                var httpReqMes = new HttpRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("https://mystatfiles.itstep.org/index.php"),
                    Headers    =
                    {
                        { "Authorization", $"Bearer {AccessToken}"                                 },
                        { "Accept",        "application/json, text/plain, */*"                     },
                        { "Origin",        "https://mystat.itstep.org"                             },
                        { "Referer",       "https://mystat.itstep.org/en/main/homework/page/index" }
                    },
                    Content = form
                };

                HttpResponseMessage responseMessage = httpClient.SendAsync(httpReqMes).Result;
                responseMessage.EnsureSuccessStatusCode();

                dynamic filenameServ = JsonConvert.DeserializeObject(responseMessage.Content.ReadAsStringAsync().Result);
                var     content      = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("filename", $"{filenameServ.name}"),
                    new KeyValuePair <string, string>("id", $"{homework.Id}")
                });

                var create = new HttpRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("https://msapi.itstep.org/api/v1/homework/operations/create"),
                    Headers    =
                    {
                        { "Authorization", $"Bearer {AccessToken}"             },
                        { "Accept",        "application/json, text/plain, */*" },
                        { "Origin",        "https://mystat.itstep.org"         },
                    },
                    Content = content
                };

                HttpResponseMessage responseMessage2 = httpClient.SendAsync(create).Result;
                responseMessage2.EnsureSuccessStatusCode();

                httpClient.Dispose();
                Logger.Log("Uploading file DONE.", ConsoleColor.Green);
                return(true);
            }
            catch (Exception e)
            {
                Logger.Log(e.Message, ConsoleColor.Red);
                return(false);
            }
        }
Пример #3
0
 public async Task UploadHomeworkFileAsync(HomeworkEntity homework, string pathToFile)
 {
     await Task.Run(() => UploadHomeworkFile(homework, pathToFile));
 }
Пример #4
0
 public async Task DownloadHomeworkFileAsync(HomeworkEntity homework, string downloadPath)
 {
     await Task.Run(() => DownloadHomeworkFile(homework, downloadPath));
 }