void backgroundWorkerUploadImage_ProgressChanged(object sender, ProgressChangedEventArgs e) { string link = e.UserState as string; AppNetDotNet.Model.File file = e.UserState as AppNetDotNet.Model.File; if (file != null && e.ProgressPercentage == 100) { autoCompeteTextbox_post.Text = autoCompeteTextbox_post.Text.TrimEnd(); autoCompeteTextbox_post.Text += " photos.app.net/{post_id}/1"; autoCompeteTextbox_post.Text += link; buttonAddImage.IsEnabled = false; toBeEmbeddedFile = file; } else if (!string.IsNullOrEmpty(link) && e.ProgressPercentage == 0) { MessageBox.Show(link, "Error in upload"); } else { MessageBox.Show("Unknown Error in upload"); } this.Dispatcher.BeginInvoke(new Action(() => this.progressBarUploading.Visibility = Visibility.Collapsed)); //this.Dispatcher.BeginInvoke(new Action(() => this.labelUploading.Visibility = Visibility.Collapsed)); }
public static Tuple<File, ApiCallResponse> getFile(string access_token, string file_id, FileQueryParameters parameter = null) { ApiCallResponse apiCallResponse = new ApiCallResponse(); File file = new File(); try { if (string.IsNullOrEmpty(access_token)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter access_token"; return new Tuple<File, ApiCallResponse>(file, apiCallResponse); } if (string.IsNullOrEmpty(file_id)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter file_id"; return new Tuple<File, ApiCallResponse>(file, apiCallResponse); } string requestUrl = Common.baseUrl + "/stream/0/files/" + file_id; Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("Authorization", "Bearer " + access_token); Helper.Response response = Helper.SendGetRequest(requestUrl, headers); return Helper.getData<File>(response); } catch (Exception exp) { apiCallResponse.success = false; apiCallResponse.errorMessage = exp.Message; apiCallResponse.errorDescription = exp.StackTrace; } return new Tuple<File, ApiCallResponse>(file, apiCallResponse); }
private void Button_Click_1(object sender, RoutedEventArgs e) { files.Clear(); textblock_filename.Text = ""; textblock_id.Text = ""; textblock_kind.Text = ""; currentFile = null; textblock_headerMetadata.Text = ""; Tuple<List<File>,ApiCallResponse> receivedFiles = Files.getMyFiles(userToken); if (receivedFiles.Item2.success) { foreach (File file in receivedFiles.Item1) { files.Add(file); } } }
private void button_getFile_Click_1(object sender, RoutedEventArgs e) { Button button = sender as Button; if (button != null) { File file = button.CommandParameter as File; if (file != null) { image_fullSize.Source = null; Tuple<File,ApiCallResponse> fileContent = AppNetDotNet.ApiCalls.Files.getFile(userToken, file.id); if (fileContent.Item2.success) { textblock_filename.Text = file.name; textblock_id.Text = file.id; textblock_kind.Text = file.kind; textblock_headerMetadata.Text = "Metadata"; currentFile = file; if (!string.IsNullOrEmpty(fileContent.Item1.mime_type)) { if (fileContent.Item1.mime_type.StartsWith("image")) { BitmapImage bi = new BitmapImage(); try { bi.BeginInit(); bi.UriSource = new Uri(fileContent.Item1.url, UriKind.Absolute); bi.EndInit(); } catch { return; } image_fullSize.Source = bi; } } } } } }
public static Tuple<File, ApiCallResponse> delete(string access_token, string file_id, FileQueryParameters parameter = null) { ApiCallResponse apiCallResponse = new ApiCallResponse(); File file = new File(); if (string.IsNullOrEmpty(access_token)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter access_token"; return new Tuple<File, ApiCallResponse>(new File(), apiCallResponse); } if (string.IsNullOrEmpty(file_id)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter file_id"; return new Tuple<File, ApiCallResponse>(new File(), apiCallResponse); } try { string requestUrl = Common.baseUrl + "/stream/0/files/" + file_id; Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("Authorization", "Bearer " + access_token); Helper.Response response = Helper.SendDeleteRequest( requestUrl, headers ); return Helper.getData<File>(response); } catch (WebException e) { WebResponse response = e.Response; HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); System.IO.Stream data = response.GetResponseStream(); string text = new System.IO.StreamReader(data).ReadToEnd(); Console.WriteLine(text); } catch (Exception exp) { apiCallResponse.success = false; apiCallResponse.errorMessage = exp.Message; apiCallResponse.errorDescription = exp.StackTrace; } return new Tuple<File, ApiCallResponse>(file, apiCallResponse); }
public static Tuple<File, ApiCallResponse> update(string access_token, string file_id, string name = null, List<Annotation> annotations = null, FileQueryParameters parameter = null) { ApiCallResponse apiCallResponse = new ApiCallResponse(); File file = new File(); if (string.IsNullOrEmpty(access_token)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter access_token"; return new Tuple<File, ApiCallResponse>(new File(), apiCallResponse); } if (string.IsNullOrEmpty(file_id)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter file_id"; return new Tuple<File, ApiCallResponse>(new File(), apiCallResponse); } if (string.IsNullOrEmpty(name) && annotations == null) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter name or annotations - at least one of those must be set"; return new Tuple<File, ApiCallResponse>(new File(), apiCallResponse); } try { FileUpdateParameters tempFile = new FileUpdateParameters(); tempFile.name = name; tempFile.annotations = annotations; string requestUrl = Common.baseUrl + "/stream/0/files/" + file_id; JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; string jsonString = JsonConvert.SerializeObject(tempFile, Formatting.None, settings); Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("Authorization", "Bearer " + access_token); Helper.Response response = Helper.SendPutRequestStringDataOnly( requestUrl, jsonString, headers, true, contentType: "application/json"); return Helper.getData<File>(response); } catch (WebException e) { WebResponse response = e.Response; HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); System.IO.Stream data = response.GetResponseStream(); string text = new System.IO.StreamReader(data).ReadToEnd(); Console.WriteLine(text); } catch (Exception exp) { apiCallResponse.success = false; apiCallResponse.errorMessage = exp.Message; apiCallResponse.errorDescription = exp.StackTrace; } return new Tuple<File, ApiCallResponse>(file, apiCallResponse); }
public static Tuple<File, ApiCallResponse> create(string access_token, string local_file_path = null, string name = null, string type = null, List<Annotation> annotations = null, string kind = null, FileQueryParameters parameter = null) { ApiCallResponse apiCallResponse = new ApiCallResponse(); File file = new File(); if (string.IsNullOrEmpty(access_token)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter access_token"; return new Tuple<File, ApiCallResponse>(new File(), apiCallResponse); } if (string.IsNullOrEmpty(type)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter type"; return new Tuple<File, ApiCallResponse>(new File(), apiCallResponse); } try { FileCreateParameters tempFile = new FileCreateParameters(); if (!string.IsNullOrEmpty(local_file_path)) { tempFile.name = System.IO.Path.GetFileName(local_file_path); } if (!string.IsNullOrEmpty(name)) { tempFile.name = name; } tempFile.type = type; tempFile.annotations = annotations; tempFile.kind = kind; string requestUrl = Common.baseUrl + "/stream/0/files/"; JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; string jsonString = JsonConvert.SerializeObject(tempFile, Formatting.None, settings); Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("Authorization", "Bearer " + access_token); Helper.Response response = Helper.SendPostRequestStringDataOnly( requestUrl, jsonString, headers, true, contentType: "application/json"); Tuple<File, ApiCallResponse> returnValue = Helper.getData<File>(response); if (returnValue.Item2.success) { if (!string.IsNullOrEmpty(local_file_path)) { setContent(access_token, returnValue.Item1.id, local_file_path); return returnValue; } } } catch (WebException e) { WebResponse response = e.Response; HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); System.IO.Stream data = response.GetResponseStream(); string text = new System.IO.StreamReader(data).ReadToEnd(); Console.WriteLine(text); } catch (Exception exp) { apiCallResponse.success = false; apiCallResponse.errorMessage = exp.Message; apiCallResponse.errorDescription = exp.StackTrace; } return new Tuple<File, ApiCallResponse>(file, apiCallResponse); }
public static ApiCallResponse setContent(string access_token, string file_id, string local_file_path, string file_type = null, string mimeType = null, FileQueryParameters parameter = null) { ApiCallResponse apiCallResponse = new ApiCallResponse(); File file = new File(); try { if (string.IsNullOrEmpty(access_token)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter access_token"; return apiCallResponse; } if (string.IsNullOrEmpty(local_file_path)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter local_file_path"; return apiCallResponse; } if (string.IsNullOrEmpty(file_id)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "Missing parameter file_id"; return apiCallResponse; } string requestUrl = Common.baseUrl + "/stream/0/files/" + file_id + "/content"; Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("Authorization", "Bearer " + access_token); if (!System.IO.File.Exists(local_file_path)) { apiCallResponse.success = false; apiCallResponse.errorMessage = "File not found"; apiCallResponse.errorDescription = "The local file has not been found at " + local_file_path; } else { System.IO.FileStream fs = new System.IO.FileStream(local_file_path, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); fs.Close(); if (string.IsNullOrEmpty(mimeType)) { mimeType = Helper.getMimeFromFile(local_file_path); } Helper.Response response = Helper.SendPutRequestBinaryDataOnly( requestUrl, data, headers, true, contentType: mimeType ); apiCallResponse = new ApiCallResponse(response); return apiCallResponse; } } catch (WebException e) { WebResponse response = e.Response; HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); System.IO.Stream data = response.GetResponseStream(); string text = new System.IO.StreamReader(data).ReadToEnd(); Console.WriteLine(text); } catch (Exception exp) { apiCallResponse.success = false; apiCallResponse.errorMessage = exp.Message; apiCallResponse.errorDescription = exp.StackTrace; } return apiCallResponse; }