예제 #1
0
 /// <summary>
 /// This method findUserId method returns id of given username
 /// </summary>
 /// <param name="username"></param>
 /// <returns></returns>
 public int findUserId(string username)
 {
     DBManager.UserM u    = new DBManager.UserM();
     Model.UserModel user = u.getUserRecord(connection, username);
     if (user == null)
     {
         return(-1);
     }
     return(user.getUid());
 }
예제 #2
0
        /// <summary>
        /// This method should return TRUE if user account created successfully
        /// FALSE if user record already exist in database
        /// THE CONTAINER IS NAMED AS USERID [unique identifier]
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Boolean createUser(String fullname, String username, String password)
        {
            Model.UserModel user = new Model.UserModel();
            DBManager.UserM u    = new DBManager.UserM();
            user = u.getUserRecord(connection, username);

            //if user already exist, false
            if (!(user == null))
            {
                return(false);
            }

            //Insert record into USERS table
            u.insertIntoUsers(connection, fullname, username, password);
            user = u.getUserRecord(connection, username);

            //default private container created
            //Container get created by the unique user id
            Console.WriteLine("Container created with " + user.getUid() + " name");

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer myContainer = BlobStorageManager.BlobStorage.getCloudBlobContainer((user.getUid()).ToString());
            myContainer.CreateIfNotExists();

            String containerName = (user.getUid()).ToString();

            //Insert record into CONTAINERS table
            DBManager.ResourceM r = new DBManager.ResourceM();
            Console.WriteLine(containerName + "////" + user.getUid());
            Model.AzureContainerModel re = new Model.AzureContainerModel();
            re.setOwner(user.getUid());
            re.setContainerName(containerName);
            re.setGivenName(user.getEmailId());  //Changed here since server front end is considering private container name as user email id
            Boolean res = r.insertIntoResources(connection, re);

            return(res);
        }
예제 #3
0
        /// <summary>
        /// This method returns a list of strings as follows:
        ///   > if 'path' is empty, a list of all containers that username can access
        ///   > else path format userid:containername, it will list all files in given container
        ///   THROW UNAUTHORIZEDACCESSEXCEPTION IF USER HAS NO RIGHT TO ACCESS THE MENTIONED PATH
        /// </summary>
        /// <param name="username"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public String[] list(String emailID, String givencontainerName)  //path containerName
        {
            DBManager.ResourceM resource = new DBManager.ResourceM();
            DBManager.UserM     user     = new DBManager.UserM();
            Model.UserModel     u        = new Model.UserModel();
            u = user.getUserRecord(connection, emailID);

            if (String.IsNullOrEmpty(givencontainerName))
            {
                Console.WriteLine("LIST ALL CONTAINERS FOR {0}", u.getUid());
                DBManager.CListM cl = new DBManager.CListM();
                String[]         sharedContainers = null;
                sharedContainers = cl.getSharedContainersWithUser(connection, u.getUid());//CONTAINERNAME:OWNERFULLNAME:GIVENCONTAINERNAME
                //Get the list of containers user owns
                String[] containers = null;
                containers = resource.getContainersOwnedByUser(connection, u.getUid()); //<CONTAINERNAME:GIVENNAME>
                Console.WriteLine("Total containers OWNED BY user : {0}", (containers.Length));
                //Console.WriteLine("Total containers SHARED WITH user: {0}", sharedContainers.Length); //sometimes cause problem no share container exists
                String[] allContainers;

                if (sharedContainers == null)
                {
                    allContainers = new String[containers.Length];
                    containers.CopyTo(allContainers, 0);
                }
                else
                {
                    allContainers = new String[containers.Length + sharedContainers.Length];
                    containers.CopyTo(allContainers, 0);
                    sharedContainers.CopyTo(allContainers, containers.Length);
                }

                return(allContainers);
            }

            Console.WriteLine("GIVEN CONT: " + givencontainerName + " uid: " + u.getUid());

            string containerName;

            if (givencontainerName.Equals(emailID)) //Considering this new change, I'm going to map containername with user email id
            {
                containerName = u.getUid().ToString();
            }
            else
            {
                Resource r           = new Resource();
                int      containerID = r.getContainerID(u.getUid(), givencontainerName);
                if (containerID == -1)
                {
                    throw new DBLikeExceptions.CloudContainerNotFoundException();
                }
                DBManager.ResourceM rmgr = new DBManager.ResourceM();

                Model.AzureContainerModel cont = rmgr.getResourceById(connection, containerID);
                if (cont == null)
                {
                    return(new String[0]);
                }
                containerName = cont.getContainerName();
            }



            Console.WriteLine(containerName);
            //Obtain reference of user's blob container
            CloudBlobContainer myContainer = BlobStorageManager.BlobStorage.getCloudBlobContainer(containerName);

            List <String> blobs = new List <String>();

            //List the files(or blobs) contained by folder(or container)

            foreach (var blobItem in myContainer.ListBlobs())
            {
                //Console.WriteLine(blobItem.Uri+",");
                if (blobItem is CloudBlobDirectory)
                {
                    addRecursive((CloudBlobDirectory)blobItem, blobs);
                }
                else
                {
                    addFile(blobItem.Uri.LocalPath, blobs);
                }
            }

            String[] blobnames = blobs.ToArray();
            return(blobnames);
        }
예제 #4
0
        /// <summary>
        /// This method takes (path{containerID:path}, local path) and uploads the file to blob storage
        ///   THROW UNAUTHORIZEDACCESSEXCEPTION IF USER HAS NO RIGHT TO ACCESS THE MENTIONED PATH
        /// NOTE---------------->>>>>>>>>>
        /// For uploading, first need to pass the containerID in path which can be obtained by using Resource:getContainerID()
        ///   then check for write permission using Resource:canWrite() then call this operation
        ///   For example :
        ///   Path <9043: bar/foo.txt> means we want to create a folder named bar and save foo.txt file in container 9043
        /// </summary>
        /// <param name="username"></param>
        /// <param name="path"></param>
        /// <param name="data"></param>
        public void upload(UPLOAD_INFO info, String localpath)
        {
            try
            {
                String          username = info.username;
                String          path     = info.path;
                DBManager.UserM umgr     = new DBManager.UserM();
                Model.UserModel user     = new Model.UserModel();
                user = umgr.getUserRecord(connection, username);
                //Break the path <containerid:path>
                String[] pathTokens = path.Split(':');

                String destinationCont = pathTokens[0];
                String destinationPath = pathTokens[1];
                String mycontainer     = destinationCont;

                CloudBlobContainer destContainer;
                if (username.Equals(destinationCont))
                {
                    Console.WriteLine("File.cs#upload(): Writing to user private area.");
                    destContainer = BlobStorageManager.BlobStorage.getCloudBlobContainer(user.getUid().ToString());
                }
                else
                {
                    DBManager.ResourceM contDetail = new DBManager.ResourceM();
                    Resource            res        = new Resource();
                    int containerId = res.getContainerID(user.getUid(), destinationCont);

                    if (containerId == -1)
                    {
                        throw new DBLikeExceptions.CloudContainerNotFoundException("File.cs#upload: no container");
                    }
                    Model.AzureContainerModel container = contDetail.getResourceById(connection, containerId);
                    Console.WriteLine("File.cs#upload(): Writing to shared container {0}", container.getContainerName());
                    destContainer = BlobStorageManager.BlobStorage.getCloudBlobContainer(container.getContainerName());
                }

                /*
                 * Console.WriteLine("ResourceID:" + destinationCont);
                 *
                 * Resource res = new Resource();
                 *
                 * if (container.getOwner() != user.getUid())  //user is not owner
                 * {
                 *  if (!res.canWrite(user.getUid(), Int32.Parse(destinationCont)))   //not having write permission
                 *  {
                 *      throw new DBLikeExceptions.UnauthorizedAccessException();
                 *  }
                 * }
                 * mycontainer = container.getContainerName();
                 * */

                //CloudBlobContainer myContainer = BlobStorageManager.BlobStorage.getCloudBlobContainer(mycontainer);
                String blobName = String.Format(destinationPath);

                CloudBlockBlob file = destContainer.GetBlockBlobReference(blobName);

                using (FileStream fstream = new FileStream(localpath, FileMode.Open))
                {
                    file.UploadFromStream(fstream);
                }

                BlobStorageManager.BlobFileHandler bhandler = new BlobStorageManager.BlobFileHandler();
                //fstream = new FileStream(localpath, FileMode.Open);
                //file.Properties.
                file.Metadata["HashValue"]        = info.curHash;
                file.Metadata["ClientModifyTime"] = info.utcTime.Ticks.ToString();
                file.SetMetadata();
                file.Properties.ContentType = "file/active";
                file.SetProperties();
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException ex)
            {
                Console.WriteLine("upload method in User class says-> " + ex);
            }
        }