Esempio n. 1
0
        public void UpdateUserAvatar(byte[] buffer, string fileName, string userId)
        {
            string fileExtension = Path.GetExtension(fileName);

            fileName = Guid.NewGuid() + fileExtension;


            //should be information of registered user
            UserProfile user = Database.UserRepository.Find(userId);

            if (user == null)
            {
                throw new NullReferenceException("Invalid User id");
            }

            var localpath = Path.Combine(PhysicalDirectory.ImagesStoreFolder, user.Id, fileName);

            //path to image location on server
            var path = Path.Combine(PhysicalDirectory.CurrentServerLocation, localpath);


            try
            {
                PhysicalDirectory.WriteImageOnServerPhysically(buffer, path);

                //deletes old image if exists
                if (!string.IsNullOrEmpty(user.AvatarUrl))
                {
                    var oldpath = Path.Combine(PhysicalDirectory.CurrentServerLocation, user.Id, user.AvatarUrl);
                    PhysicalDirectory.DeleleImageFromServerPhysically(oldpath);
                }

                user.AvatarUrl = localpath;
                Database.UserRepository.Update(user);
                Database.Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        public void AddImage(UploadedImage image)
        {
            string fileExtension = Path.GetExtension(image.FileName);
            string fileName      = Guid.NewGuid() + fileExtension;

            UserProfile user = Database.UserRepository.Find(image.UserId);

            if (user == null)
            {
                throw new NullReferenceException("Invalid User id");
            }

            //will be written into photo data
            var localpath     = Path.Combine(PhysicalDirectory.ImagesStoreFolder, user.Id, fileName);
            var fullImagePath = Path.Combine(PhysicalDirectory.CurrentServerLocation, localpath);

            PhysicalDirectory.WriteImageOnServerPhysically(image.Buffer, fullImagePath);

            Image newImage = new Image
            {
                User        = user,
                LocalPath   = localpath,
                ImageName   = fileName,
                Description = image.Description,
            };

            newImage.Tags = AddTags(image.Tags, newImage);

            try
            {
                Database.ImageRepository.Add(newImage);
                Database.Commit();
            }
            catch (Exception)
            {
                //Rolling Back happend an error
                PhysicalDirectory.DeleleImageFromServerPhysically(fullImagePath);
                throw;
            }
        }