public void Delete(string name, FilesType type) { if (File.Exists($@"{type.ToString()}\{name}")) { File.Delete($@"{type.ToString()}\{name}"); } }
public byte[] Download(string name, FilesType type) { if (File.Exists($@"{type.ToString()}\{name}")) { return(File.ReadAllBytes($@"{type.ToString()}\{name}")); } return(null); }
public bool Upload(byte[] file, string name, FilesType type) { try { string fileName = ""; if (!Directory.Exists(type.ToString())) { Directory.CreateDirectory(type.ToString()); } fileName = $@"{type.ToString()}\{name}"; using (FileStream fs = File.Create(fileName)) { fs.Write(file, 0, file.Length); fs.Dispose(); } return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
/// <summary> /// Downloads an image from server and presents it. /// </summary> /// <param name="id">Images id.</param> /// <param name="defaultPath">Default path.</param> /// <param name="tempPath">The location of temporary files.</param> /// <param name="parent">The panel where an image is supposed to be added.</param> /// <param name="type">Data type.</param> void AddImage(int id, string defaultPath, string tempPath, Panel parent, ServerData type, bool edit) { Thread thd = new Thread(new ThreadStart(() => { Dispatcher.InvokeAsync(new Action(() => { string img = _proxy.GetItemPropertyAsync(id, type, PropertyData.Imgpath).Result ?? defaultPath; if (img == null) { return; } if (img == defaultPath) { parent.Children.Add(new Image { Source = new BitmapImage(new Uri($"pack://*****:*****@"Temp\{tempPath}")) { Directory.CreateDirectory($@"Temp\{tempPath}"); } FilesType filesType = FilesType.Avatars; switch (type) { case ServerData.Video: filesType = FilesType.VideosImages; break; case ServerData.Book: filesType = FilesType.BooksImages; break; case ServerData.User: filesType = FilesType.Avatars; break; case ServerData.Word: filesType = FilesType.WordsImages; break; } byte[] res = _proxy.Download(img, filesType); if (res != null) { try { using (FileStream fs = File.OpenWrite($@"Temp\{tempPath}\{img}")) { Task.WaitAny(fs.WriteAsync(res, 0, res.Length)); fs.Dispose(); } } catch (IOException) { if (edit) { if (MessageBox.Show("The data have been uploaded to the server. It will be updated the next time you come.\nDo you want to restart now?", "Check next time", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { Process.GetCurrentProcess().Kill(); } } } Image tmp = new Image { Height = 110 }; FormData.SetImage($@"pack://siteoforigin:,,,/Temp\{tempPath}\{img}", tmp); parent.Children.Insert(0, tmp); } } })); })) { IsBackground = true }; thd.Start(); }
/// <summary> /// Downloads an image from server and presents it. /// </summary> /// <param name="id">Images id.</param> /// <param name="defaultPath">Default path.</param> /// <param name="height">Images height.</param> /// <param name="tempPath">The location of temporary files.</param> /// <param name="parent">The panel where an image is supposed to be added.</param> /// <param name="type">Data type.</param> void AddImage(int id, string defaultPath, string tempPath, int height, Panel parent, ServerData type, bool edit) { Thread thd = new Thread(new ThreadStart(() => { Dispatcher.InvokeAsync(new Action(() => { string img = _proxy.GetItemPropertyAsync(id, type, PropertyData.Imgpath).Result ?? defaultPath; if (img == null) { return; } if (img == defaultPath) { parent.Children.Add(new Image { Source = new BitmapImage(new Uri($"pack://*****:*****@"Temp\{tempPath}")) { Directory.CreateDirectory($@"Temp\{tempPath}"); } FilesType filesType = FilesType.Avatars; switch (type) { case ServerData.Video: filesType = FilesType.VideosImages; break; case ServerData.Book: filesType = FilesType.BooksImages; break; case ServerData.User: filesType = FilesType.Avatars; break; case ServerData.Word: filesType = FilesType.WordsImages; break; } byte[] res = _proxy.Download(img, filesType); if (res != null) { try { using (FileStream fs = File.OpenWrite($@"Temp\{tempPath}\{img}")) { Task.WaitAny(fs.WriteAsync(res, 0, res.Length)); fs.Dispose(); } } catch (IOException) { } Image tmp = new Image { Height = height }; FormData.SetImage($@"pack://siteoforigin:,,,/Temp\{tempPath}\{img}", tmp); parent.Children.Insert(0, tmp); } } })); })) { IsBackground = true }; thd.Start(); }