Exemple #1
0
        public static async Task HandleViewCommentsPhoto(FileServer.Server server, CommunicationClient client)
        {
            var photoIdParsed = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength));

            ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.ListComments,
                                                client.StreamCommunication);

            var photo = new PhotoDto()
            {
                Id = photoIdParsed
            };
            var comments = await server.Service.GetCommentsAsync(photo);

            var length = comments.Count() * (User.UserEmailLength + User.UserNameLength + Comment.CommentLength);

            var data = ConversionHandler.ConvertIntToBytes(length);

            client.StreamCommunication.Write(data);

            comments.ToList().ForEach((elem) =>
            {
                var comment = new Comment()
                {
                    Message     = elem.Message,
                    Commentator = new User()
                    {
                        Email = elem.UserEmail,
                        Name  = elem.UserName
                    }
                };
                ProtocolHelpers.SendCommentData(client.StreamCommunication, comment);
            });

            loggerService.SendMessages("Comments listed correctly");
        }
Exemple #2
0
        public static async Task HandleUploadPhoto(Server server, CommunicationClient client)
        {
            var name      = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoNameLength));
            var extension = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoExtensionLength));
            var fileSize  = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength));

            if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(extension))
            {
                ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Input Error");
            }

            var photo = new PhotoDto()
            {
                Name      = name,
                Extension = extension,
                FileSize  = fileSize,
                UserEmail = client.User.Email
            };

            await server.Service.UploadPhotoAsync(photo);

            var fileName = $"{PhotosPath}\\Image_{photo.Id}{extension}";

            await FileHandler.ReceiveFileWithStreams(fileSize, fileName, client.StreamCommunication);

            loggerService.SendMessages("Image uploaded");

            ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Added succesfully");
        }
Exemple #3
0
        public static async Task HandleCommentPhoto(Server server, CommunicationClient client)
        {
            var photoIdParsed = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength));
            var message       = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Comment.CommentLength));

            if (string.IsNullOrWhiteSpace(message))
            {
                ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Input Error");
            }

            var comment = new CommentDto()
            {
                PhotoId   = photoIdParsed,
                Message   = message,
                UserEmail = client.User.Email,
            };

            await server.Service.AddCommentAsync(comment);

            loggerService.SendMessages("Image commented");

            ProtocolHelpers.SendMessageCommand(ProtocolConstants.ResponseCommands.Ok, client, "Added Sucessfully");
        }
Exemple #4
0
        public static async Task <List <Photo> > HandleViewPhotos(Client client)
        {
            ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.VIEW_PHOTOS, client.StreamCommunication);

            var response        = ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength));
            var responseCommand = ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength));

            var data = await client.StreamCommunication.ReadAsync(ProtocolConstants.IntegerTypeLength);

            var dataLength = ConversionHandler.ConvertBytesToInt(data);

            var result = new List <Photo>();

            while (dataLength != 0)
            {
                var email       = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength));
                var photoId     = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength));
                var photoName   = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoNameLength));
                var extension   = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(Photo.PhotoExtensionLength));
                var photoLength = ConversionHandler.ConvertBytesToLong(await client.StreamCommunication.ReadAsync(ProtocolConstants.LongTypeLength));

                dataLength -= User.UserEmailLength + ProtocolConstants.LongTypeLength + Photo.PhotoNameLength + Photo.PhotoExtensionLength + ProtocolConstants.LongTypeLength;
                result.Add(new Photo()
                {
                    User = new User()
                    {
                        Email = email
                    },
                    Id        = photoId,
                    Name      = photoName,
                    FileSize  = photoLength,
                    Extension = extension,
                });
            }

            return(result);
        }