コード例 #1
0
        /// <summary>
        /// Sets up all necessary blob containers (if they do not exist)
        /// </summary>
        public static async Task InitializeBlobContainersAsync(this ApexVisualManager avm)
        {
            CloudBlobClient cbc = GetCloudBlobClient(avm.AzureStorageConnectionString);
            await cbc.GetContainerReference("sessions").CreateIfNotExistsAsync();

            await cbc.GetContainerReference("userphotos").CreateIfNotExistsAsync();

            await cbc.GetContainerReference("activitylogs").CreateIfNotExistsAsync();

            await cbc.GetContainerReference("messagesubmissions").CreateIfNotExistsAsync();
        }
コード例 #2
0
        /// <summary>
        /// This uploads the image to Azure and then provides you with the unique ID that the image is called. You can then use this ID by plugging into the Apex Visual User account as the image ID.
        /// </summary>
        public static async Task <string> UploadProfilePictureAsync(this ApexVisualManager avm, Stream image_stream)
        {
            //Get the container
            CloudBlobClient    cbc  = GetCloudBlobClient(avm.AzureStorageConnectionString);
            CloudBlobContainer cont = cbc.GetContainerReference("userphotos");
            await cont.CreateIfNotExistsAsync();

            //Get a name for it and upload it
            string         ToReturnId = Guid.NewGuid().ToString();
            CloudBlockBlob blb        = cont.GetBlockBlobReference(ToReturnId);
            await blb.UploadFromStreamAsync(image_stream);

            return(ToReturnId);
        }
コード例 #3
0
        public static async Task <string> DownloadMessageSubmissionBodyAsync(this ApexVisualManager avm, Guid id)
        {
            CloudBlobClient    cbc  = GetCloudBlobClient(avm.AzureStorageConnectionString);
            CloudBlobContainer cont = cbc.GetContainerReference("messagesubmissions");
            CloudBlockBlob     blb  = cont.GetBlockBlobReference(id.ToString());

            if (blb.Exists() == false)
            {
                throw new Exception("Message submission body with ID '" + id.ToString() + "' does not exist in blob storage.");
            }

            string content = await blb.DownloadTextAsync();

            return(content);
        }
コード例 #4
0
        public static async Task <Guid> CascadeUploadMessageSubmissionAsync(this ApexVisualManager avm, MessageSubmission msg)
        {
            Guid ToUseAndReturn = Guid.NewGuid();

            //Upload the Message Submission itself to sql
            Guid id_SQL = await avm.UploadMessageSubmissionAsync(msg, ToUseAndReturn);

            //Upload the body
            Guid id_BLOB = await avm.UploadMessageSubmissionBodyAsync(msg.Body, ToUseAndReturn);

            if (id_SQL != id_BLOB)
            {
                throw new Exception("The ID of the MessageSubmission in SQL is not identical to the BLOB that contains the body.");
            }

            return(id_SQL);
        }
コード例 #5
0
        // UTLITY FUNCTIONS BELOW
        private static async Task <string[]> GetBlobNamesInContainerAsync(this ApexVisualManager avm, string container_name)
        {
            CloudBlobClient cbc = GetCloudBlobClient(avm.AzureStorageConnectionString);

            List <string> ToReturn = new List <string>();

            CloudBlobContainer cont = cbc.GetContainerReference(container_name);
            await cont.CreateIfNotExistsAsync();

            IEnumerable <IListBlobItem> blobs = cont.ListBlobs();

            foreach (IListBlobItem bi in blobs)
            {
                CloudBlockBlob blb = (CloudBlockBlob)bi;
                ToReturn.Add(blb.Name);
            }

            return(ToReturn.ToArray());
        }
コード例 #6
0
        public static async Task <Guid> UploadMessageSubmissionBodyAsync(this ApexVisualManager avm, string body, Guid?as_id = null)
        {
            //If an ID is supplied, use that. If it is not supplied, use a random one.
            Guid g = Guid.NewGuid();

            if (as_id != null)
            {
                g = as_id.Value;
            }

            CloudBlobClient    cbc  = GetCloudBlobClient(avm.AzureStorageConnectionString);
            CloudBlobContainer cont = cbc.GetContainerReference("messagesubmissions");
            await cont.CreateIfNotExistsAsync();

            //Upload it
            CloudBlockBlob blb = cont.GetBlockBlobReference(g.ToString());
            await blb.UploadTextAsync(body);

            return(g);
        }
コード例 #7
0
        public static async Task <Stream> DownloadProfilePictureAsync(this ApexVisualManager avm, string id)
        {
            //Get the container
            CloudBlobClient    cbc  = GetCloudBlobClient(avm.AzureStorageConnectionString);
            CloudBlobContainer cont = cbc.GetContainerReference("userphotos");
            await cont.CreateIfNotExistsAsync();

            //Get the profile picture. Throw an error if it does not exist
            CloudBlockBlob blb = cont.GetBlockBlobReference(id);

            if (blb.Exists() == false)
            {
                throw new Exception("User photo with ID '" + id + "' does not exist.");
            }

            //Download the blob contents
            MemoryStream ms = new MemoryStream();
            await blb.DownloadToStreamAsync(ms);

            ms.Position = 0;

            return(ms);
        }
コード例 #8
0
        public static async Task UploadSessionDataAsync(this ApexVisualManager avm, List <byte[]> session_data)
        {
            //Get unique session
            string file_name = "";

            try
            {
                Packet p = new Packet();
                p.LoadBytes(session_data[0]);
                file_name = p.UniqueSessionId.ToString();
            }
            catch
            {
                throw new Exception("Fatal error while getting unique session ID.");
            }

            CloudBlobClient    cbc  = GetCloudBlobClient(avm.AzureStorageConnectionString);
            CloudBlobContainer cont = cbc.GetContainerReference("sessions");
            await cont.CreateIfNotExistsAsync();

            //Serialize to a Stream
            MemoryStream   ms  = new MemoryStream();
            StreamWriter   sw  = new StreamWriter(ms);
            JsonTextWriter jtw = new JsonTextWriter(sw);
            JsonSerializer js  = new JsonSerializer();

            js.Serialize(jtw, session_data);
            jtw.Flush();
            await ms.FlushAsync();

            ms.Position = 0;


            //Upload
            CloudBlockBlob blb = cont.GetBlockBlobReference(file_name);
            await blb.UploadFromStreamAsync(ms);
        }
コード例 #9
0
        public static async Task <List <byte[]> > DownloadSessionDataAsync(this ApexVisualManager avm, string sessionID)
        {
            CloudBlobClient    cbc  = GetCloudBlobClient(avm.AzureStorageConnectionString);
            CloudBlobContainer cont = cbc.GetContainerReference("sessions");
            await cont.CreateIfNotExistsAsync();

            CloudBlockBlob blb = cont.GetBlockBlobReference(sessionID);

            if (blb.Exists() == false)
            {
                throw new Exception("Unable to find session with title '" + sessionID + "'.");
            }

            //Download the Stream
            MemoryStream ms = new MemoryStream();
            await blb.DownloadToStreamAsync(ms);

            ms.Position = 0;

            //Desrialize
            StreamReader   sr  = new StreamReader(ms);
            JsonTextReader jtr = new JsonTextReader(sr);
            JsonSerializer js  = new JsonSerializer();
            List <byte[]>  data_to_return;

            try
            {
                data_to_return = js.Deserialize <List <byte[]> >(jtr);
            }
            catch (Exception e)
            {
                throw new Exception("Failure while deserializing content for session '" + sessionID.ToString() + "'. Message: " + e.Message);
            }

            return(data_to_return);
        }
コード例 #10
0
        public static async Task <MessageSubmission> CascadeDownloadMessageSubmissionAsync(this ApexVisualManager avm, Guid id)
        {
            MessageSubmission ToReturn = await avm.DownloadMessageSubmissionAsync(id);

            string body = await avm.DownloadMessageSubmissionBodyAsync(id);

            ToReturn.Body = body;
            return(ToReturn);
        }