public string getDownloadKey(UPLOAD_INFO f)
        {
            string key;

            lock (pendingUploads)
            {
                while (true)
                {
                    string random1 = Path.GetRandomFileName().Substring(0, 8);
                    string random2 = Path.GetRandomFileName().Substring(0, 8);
                    key = random1 + random2;
                    try
                    {
                        //ADD TIMEOUT NOT SUPPORTED
                        pendingUploads.Add(key, f);
                        break;
                    } catch (ArgumentException) { /* keep trying */ }
                }
            }
            String containerName, blobName, containerAzureName;

            String[] arr = f.path.Split(':');
            containerName = arr[0];
            blobName      = arr[1];

            if (containerName.Equals(f.username))
            {
                containerAzureName = azureLink.findUserId(f.username).ToString();
            }
            else
            {
                int             uid = azureLink.findUserId(f.username);
                IResourceManage r   = new Resource();
                int             rid = r.getContainerID(uid, containerName);
                if (rid == -1)
                {
                    pendingUploads.Remove(key);
                    throw new DBLikeExceptions.CloudContainerNotFoundException();
                }
                if (!r.canWrite(uid, rid))
                {
                    pendingUploads.Remove(key);
                    throw new DBLikeExceptions.UnauthorizedAccessException();
                }
                /* TODO: improve this */
                containerAzureName = new DBManager.ResourceM()
                                     .getResourceById(new DBManager.DBAccess().getDBAccess(), rid)
                                     .getContainerName();
            }
            if (VALIDATE_OLD_HASH)
            {
                try
                {
                    var    blobMgr = new BlobStorageManager.BlobFileHandler();
                    String oldHash = blobMgr.getBlobMD5HashValue(containerAzureName, blobName);
                    if (oldHash != null && !oldHash.Equals(f.prevHash))
                    {
                        throw new DBLikeExceptions.HashConflictException();
                    }
                } catch (DBLikeExceptions.CloudBlobNotFoundException) { /* new file, no old hash */ }
            }
            return(key);
        }
Exemple #2
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);
            }
        }