Example #1
0
        private static Response UpdateVideoResponse(VideoUpdateRequest videoUpdateRequest)
        {
            DatabaseManager database = new DatabaseManager();

            (MySqlDataReader reader, var Connection) = database.RunQuery($"UPDATE video_details SET title='{RefineContent(videoUpdateRequest.Title)}', description='{RefineContent(videoUpdateRequest.Description)}' WHERE videoid='{videoUpdateRequest.VideoID}'");
            if (reader != null)
            {
                VideoUpdateResponse res = new VideoUpdateResponse()
                {
                    IsThumbnailUpdateRequired = videoUpdateRequest.IsThumbnailUpdateRequired,
                    ThumbnailImage            = videoUpdateRequest.VideoID,
                    Reason = "Video Updated Successfully",
                    Status = "OK"
                };

                if (videoUpdateRequest.Tags.Count >= 1)
                {
                    database.RunQuery($"DELETE FROM tag_details WHERE videoid='{videoUpdateRequest.VideoID}'");
                    StringBuilder str = new StringBuilder($"INSERT INTO tag_details (tag, videoid) VALUES ('{RefineContent(videoUpdateRequest.Tags[0])}', '{videoUpdateRequest.VideoID}')");
                    for (int i = 1; i < videoUpdateRequest.Tags.Count; i++)
                    {
                        str.Append($", ('{videoUpdateRequest.Tags[i]}', '{videoUpdateRequest.VideoID}')");
                    }
                    database.RunQuery(str.ToString());
                }
                Connection.Close();
                return(new Response()
                {
                    Type = ResponseType.VideoUpdate, Content = res, Status = "OK"
                });
            }
            Connection.Close();
            return(new Response()
            {
                Content = null, Type = ResponseType.VideoUpdate, Status = "FAIL"
            });
        }
Example #2
0
        private void _handleClient()
        {
            NetworkStream clientStream      = _client.GetStream();
            int           ConnectionTimeout = 1500; //30 Seconds

            //send acnowledgement
            Home_Page.ConnectedClient++;
            StatusManager.PushMessage("Client " + _client.Client.RemoteEndPoint.ToString() + " Connected. ", StatusType.Success);
            while (_client.Connected && MainServer.IsRunning && ConnectionTimeout > 0)
            {
                try
                {
                    while (!clientStream.DataAvailable && _client.Connected && ConnectionTimeout > 0)
                    {
                        Thread.Sleep(200);
                        ConnectionTimeout--;
                        if (!MainServer.IsRunning)
                        {
                            return;
                        }
                    }
                    byte[] buffer = new byte[65536];

                    if (clientStream.DataAvailable)
                    {
                        int    readBytes = clientStream.Read(buffer, 0, 65535);
                        string message   = Encoding.UTF8.GetString(buffer).Trim('\0');
                        //Process Message
                        Request  request = JsonConvert.DeserializeObject <Request>(message);
                        Response result  = RequestHandlers.GetResponse(request.Type, (JObject)request.Content);
                        if (result.Type == ResponseType.CreateVideo)
                        {
                            try
                            {
                                _backupClient.Start();
                                ((CreateVideoResponse)result.Content).Port = _backupClient.LocalEndpoint.ToString().Split(':')[1];
                            }
                            catch (Exception e)
                            {
                                StatusManager.PushMessage(e.Message, StatusType.Error);
                            }
                        }
                        var response = JsonConvert.SerializeObject(result);
                        //simulating heavy server load
                        //Thread.Sleep(2000);
                        buffer = Encoding.UTF8.GetBytes(response);
                        clientStream.Write(buffer, 0, response.Length);

                        if (result.Type == ResponseType.UpdateProfile)
                        {
                            if (((ProfileUpdateResponse)result.Content).RequiresImageUpdate)
                            {
                                ReceiveImage(request.Username, ".jpg");
                            }
                        }
                        else if (result.Type == ResponseType.CreateVideo)
                        {
                            Thread thread = new Thread(FileHandlingService)
                            {
                                IsBackground = true,
                                Name         = "Backup server",
                            };
                            thread.Start(((CreateVideoResponse)result.Content).VideoID);
                        }
                        else if (result.Type == ResponseType.VideoUpdate)
                        {
                            VideoUpdateResponse resp = ((VideoUpdateResponse)result.Content);
                            if (resp.IsThumbnailUpdateRequired)
                            {
                                ReceiveImage(resp.ThumbnailImage, ".jpg", false);
                            }
                        }
                        ConnectionTimeout = 1500; //timeout restored.
                    }
                }
                catch (Exception e)
                {
                    StatusManager.PushMessage(e.Message, StatusType.Error);
                    //throw;
                }
            }
            //might send a client disconnected message.
            Home_Page.ConnectedClient--;
            StatusManager.PushMessage("Client " + _client.Client.RemoteEndPoint.ToString() + " Disconnected. ", StatusType.Error);
            clientStream.Close();
        }
Example #3
0
        private async void uploadVideoButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsValid() && !IsEditVideoType)
            {
                progressRing.isActive = true;
                CreateVideoRequest request = new CreateVideoRequest()
                {
                    AuthorId   = SettingsManager.Username,
                    AuthorName = SettingsManager.FullName,
                    CourseId   = CourseID.CourseID,
                    CourseName = CourseID.CourseName,
                    Decription = descriptionTextBlock.Text,
                    Duration   = duration,
                    Height     = videoHeight,
                    Width      = videoWidth,
                    Title      = titleTextBlock.Text,
                    Tags       = new List <string>()
                };

                foreach (Tags tag in tagsContainer.Children)
                {
                    request.Tags.Add(tag.TagName);
                }
                Object resp = await ConnectionManager.SendRequestAsync(request, RequestType.CreateVideo, ResponseType.CreateVideo);

                if (resp != null)
                {
                    CreateVideoResponse response = ((JObject)resp).ToObject <CreateVideoResponse>();
                    string path = Guid.NewGuid().ToString() + ".jpg";
                    File.Copy(thumbnailPath, path);

                    Object response2 = await ConnectionManager.SendVideoFileAsync(chosenVideoPathTextBlock.Text, path, response.Port);

                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    if (response2 != null)
                    {
                        AppNotificationManager.PushMessage(new AppNotification()
                        {
                            Message = ((JObject)response2).ToObject <Acknowledge>().Reason
                        });
                    }
                    else
                    {
                        AppNotificationManager.PushMessage(new AppNotification()
                        {
                            Message = "Video Upload Failed Failed."
                        });
                    }
                    Close();
                }
                else
                {
                    errorTextBlock.Text = "Connection Error";
                }
                progressRing.isActive = false;
            }
            else if (IsValid() && IsEditVideoType)
            {
                progressRing.isActive = true;
                VideoUpdateRequest request = new VideoUpdateRequest()
                {
                    Title       = titleTextBlock.Text,
                    Description = descriptionTextBlock.Text,
                    IsThumbnailUpdateRequired = thumbnailChosen,
                    Tags = new List <string>()
                };
                foreach (Tags tags in ((WrapPanel)tagsContainer).Children)
                {
                    request.Tags.Add(tags.TagName);
                }
                Object resp = await ConnectionManager.SendRequestAsync(request, RequestType.VideoUpdate, ResponseType.Acknowledge);

                if (resp != null)
                {
                    VideoUpdateResponse ack = ((JObject)resp).ToObject <VideoUpdateResponse>();
                    if (ack.Status == "OK")
                    {
                        try
                        {
                            if (thumbnailChosen)
                            {
                                await ConnectionManager.SendFileAsync(thumbnailPath, null);
                            }
                        }catch (Exception ex)
                        {
                            AppNotificationManager.PushMessage(new AppNotification()
                            {
                                Message = ex.Message
                            });
                            Close();
                        }
                    }
                    errorTextBlock.Text = ack.Reason;
                }
                else
                {
                    errorTextBlock.Text = "Connection Error.";
                }
            }
        }
        private async void modifyVideoButton_Click(object sender, RoutedEventArgs e)
        {
            progressRing.isActive = true;
            VideoUpdateRequest request = new VideoUpdateRequest()
            {
                VideoID     = VideoID,
                Title       = titleTextBlock.Text,
                Description = descriptionTextBlock.Text,
                IsThumbnailUpdateRequired = thumbnailChosen,
                Tags = new List <string>()
            };

            foreach (Tags tags in ((WrapPanel)tagsContainer).Children)
            {
                request.Tags.Add(tags.TagName);
            }
            Object resp = await ConnectionManager.SendRequestAsync(request, RequestType.VideoUpdate, ResponseType.VideoUpdate);

            if (resp != null)
            {
                VideoUpdateResponse ack = ((JObject)resp).ToObject <VideoUpdateResponse>();
                if (ack.Status == "OK")
                {
                    if (thumbnailChosen)
                    {
                        string tempPath = "Temporary/" + Guid.NewGuid() + ".jpg";
                        if (Directory.Exists("Temporary/"))
                        {
                            Directory.CreateDirectory("Temporary/");
                        }
                        File.Copy(thumbnailPath, tempPath);
                        var result = await ConnectionManager.SendFileAsync(tempPath, null);

                        if (result != null)
                        {
                            var response = ((JObject)result).ToObject <Acknowledge>();
                            errorTextBlock.Text = response.Reason;
                            if (response.Status == "OK")
                            {
                                AppNotificationManager.PushMessage(new AppNotification()
                                {
                                    Message = response.Reason
                                });
                                Close();
                            }
                        }
                        if (File.Exists(tempPath))
                        {
                            File.Delete(tempPath);
                        }
                    }
                    AppNotificationManager.PushMessage(new AppNotification()
                    {
                        Message = ack.Reason
                    });
                    Close();
                }
                errorTextBlock.Text = ack.Reason;
            }
            else
            {
                errorTextBlock.Text = "Connection Error.";
            }
        }