# MediaManagement [C#]

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Windows;
     using System.Windows.Controls;
     using System.Windows.Data;
     using System.Windows.Documents;
     using System.Windows.Input;
     using System.Windows.Media;
     using System.Windows.Media.Imaging;
     using System.Windows.Navigation;
     using System.Windows.Shapes;
     using WpfApplication1.ServiceReference1;
     using System.Collections.ObjectModel;
     using System.Net;
     using Microsoft.Win32;
     using System.IO;

     namespace WpfApplication1
     {

         // Interaction logic for MediaManagement.xaml
         public partial class MediaManagement : UserControl
         {
             ServiceSample sample = new ServiceSample();

             public MediaManagement()
             {
                 InitializeComponent();
             }

             //Initialize the XAML object and retrieve group from server by login
             public MediaManagement(string login, string password)
             {
                 InitializeComponent();

                 //Login and retrive group
                 List<View_Group> listResult;
                 sample.GetGroup(out listResult, login, password);

                 //Bind group to TreeList
                 treeGroup.ItemsSource = TreeNode.GetTreeNode(listResult);
             }

             //Select a group and retrive coresponding medias
             private void treeGroup_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
             {
                 //Get Medias for selected group
                 List<View_Media> listResult;
                 sample.GetMedia(out listResult, (treeGroup.SelectedItem as TreeNode).Id ?? 0);

                 //Update media thumbnail path
                 listResult.ForEach(x => x.ThumbnailPath = (x.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + x.ThumbnailPath) : "");

                 //Bind media to List
                 gridMedia.ItemsSource = new ObservableCollection<View_Media>(listResult);
             }

             //Upload media by packet
             private void btnUpload_Click(object sender, RoutedEventArgs e)
             {
                 //Select a media
                 OpenFileDialog openFileDialog = new OpenFileDialog();
                 openFileDialog.Filter =
                     "All Files (*.swf, *.swa, *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.avi, *.qt, *.mpg, *.mpeg, *.m1v, *.mp2, *.wmv, *.qt, *.mov, *.mp4, *.m2v, *.3gp, *.3g2, *.divx)|*.swf; *.swa; *.bmp; *.gif; *.jpg; *.jpeg; *.png; *.avi; *.qt; *.mpg; *.mpeg; *.m1v; *.mp2; *.wmv; *.qt; *.mov; *.mp4; *.m2v; *.3gp; *.3g2; *.divx|" +
                     "Flash Files (*.swf, *.swa)|*.swf; *.swa|" +
                     "Image Files (*.bmp, *.gif, *.jpg, *.jpeg, *.png)|*.bmp; *.gif; *.jpg; *.jpeg; *.png|" +
                     "Video files (*.avi, *.qt, *.mpg, *.mpeg, *.m1v, *.mp2, *.wmv, *.qt, *.mov, *.mp4, *.m2v, *.3gp, *.3g2, *.divx)|*.avi; *.qt; *.mpg; *.mpeg; *.m1v; *.mp2; *.wmv; *.qt; *.mov; *.mp4; *.m2v; *.3gp; *.3g2; *.divx";

                 if(openFileDialog.ShowDialog() == true)
                 {
                     FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
                     Stream fileStream = fileInfo.OpenRead();

                     //Create buffer for each packet of media
                     byte[] buffer = new byte[Math.Min(fileInfo.Length, 1024 * 512)];
                     int bytesRead = 0;

                     View_Media media = null;

                     bool isFirst = true; bool isLast = false;
                     while (!isLast)
                     {
                         //Read media packet
                         bytesRead += fileStream.Read(buffer, 0, buffer.Length);
                         isLast = bytesRead >= fileInfo.Length;

                         //Send packet to server
                         sample.UploadMedia(out media, openFileDialog.SafeFileName, (treeGroup.SelectedItem as TreeNode).Id ?? 0, isFirst, isLast, buffer);

                         isFirst = false;
                     }

                     //Update media thumbnail path
                     media.ThumbnailPath = (media.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + media.ThumbnailPath) : "";

                     //Add media to collection
                     (gridMedia.ItemsSource as ObservableCollection<View_Media>).Add(media);
                 }
             }

             //Delete media
             private void btnDelete_Click(object sender, RoutedEventArgs e)
             {
                 sample.DeleteMedia(gridMedia.SelectedItem as View_Media,(treeGroup.SelectedItem as TreeNode).Id ?? 0);

                 // for refresh the list
                 treeGroup_SelectedItemChanged(null, null);
             }
         }
     }