示例#1
0
        public static string UploadAvatar(long connectionId, byte[] imageBytes)
        {
            // read path from DB and delete old avatar file
            Client   client = ClientManager.GetConnectedClient(connectionId);
            long     userId = client.UserData.UserId;
            FoolUser user   = DatabaseOperations.GetUserById(userId);

            if (string.IsNullOrEmpty(user.AvatarFile) && File.Exists(user.AvatarFile))
            {
                File.Delete(user.AvatarFile);
            }


            // find out the format of image
            string format = ByteArrayFileFormat(imageBytes);

            // create directory if not exists
            string avatarsFolderName = "avatars"; // todo load from app.config

            Directory.CreateDirectory(avatarsFolderName);

            // write to file
            string filePath = avatarsFolderName + "/" + imageBytes.GetHashCode() + format;

            var stream = File.Create(filePath);

            stream.Write(imageBytes, 0, imageBytes.Length);
            stream.Close();

            // update avatar in db
            DatabaseOperations.UpdateAvatar(userId, filePath);

            // update avatar in buffered client data
            client.UserData.AvatarFile = filePath;

            // return: send avatar exact url on server
            return(client.UserData.AvatarFileUrl);
        }
示例#2
0
 /// <summary>
 /// Registred user token constructor
 /// </summary>
 public Token(FoolUser user)
 {
     this.OwnerUser  = user;
     this.authMethod = AuthorizationMethod.Email;
 }