コード例 #1
0
        private void SendImages()
        {
            BlobConnector blobConnector   = new BlobConnector();
            List <string> blobDirectories = new List <string>();

            blobDirectories = blobConnector.ListBlobs("arimages");


            foreach (string blob in blobDirectories)
            {
                byte[]        buffer           = blobConnector.DownloadImage(blob);
                string        ret              = System.Text.Encoding.Default.GetString(buffer);
                ClientRequest retClientRequest = JsonConvert.DeserializeObject(ret) as ClientRequest;

                ServerResponse serverResponse = new ServerResponse()
                {
                    ResponseType       = "DownloadImage",
                    name               = blob,
                    featureDescription = retClientRequest.featureDescription,
                    image              = retClientRequest.image
                };

                string ret2 = JsonConvert.SerializeObject(serverResponse);
                SendThis(ret2);
            }
        }
コード例 #2
0
        /// <summary>
        /// Processes the specified json.
        /// </summary>
        /// <param name="json">The json.</param>
        private void Process(string json)
        {
            ClientRequest clientRequest = JsonConvert.DeserializeObject <ClientRequest>(json);

            switch (clientRequest.RequestType)
            {
            case RequestType.UploadImage:
            {
                BlobConnector blob = new BlobConnector();
                string        clientRequestJSON = JsonConvert.SerializeObject(clientRequest);
                byte[]        buffer            = System.Text.Encoding.UTF8.GetBytes(clientRequestJSON);
                blob.UploadImage(clientRequest.Name, new MemoryStream(buffer));

                ServerResponse serverResponse = new ServerResponse()
                {
                    ID           = clientRequest.ID,
                    Name         = clientRequest.Name,
                    ResponseType = "UploadImage"
                };

                string ret = JsonConvert.SerializeObject(serverResponse);
                SendThis(ret);

                break;
            }

            case RequestType.DownloadAllImages:
            {
                BlobConnector blobConnector = new BlobConnector();

                var blobs = blobConnector.ListBlobs("arimages");

                ServerResponse serverResponse = new ServerResponse()
                {
                    ResponseType = "DownloadAllImages",
                    Blobs        = blobs
                };

                string ret = JsonConvert.SerializeObject(serverResponse);
                SendThis(ret);

                SendImages();
            }
            break;

            case RequestType.DownloadImage:
            {
                BlobConnector  blobConnector  = new BlobConnector();
                ServerResponse serverResponse = new ServerResponse()
                {
                    ResponseType = "DownloadImage"
                };

                List <string> blobDirectories = new List <string>();
                var           blobs           = blobConnector.ListBlobs("arimages");

                // bool exists = blobDirectories.Any(s => s.Contains(clientRequest.name));
                // alternative
                int index = blobs.FindIndex(x => x.StartsWith(clientRequest.Name));

                if (index > -1)
                {
                    byte[]        buffer           = blobConnector.DownloadImage(clientRequest.Name);
                    string        ret              = System.Text.Encoding.Default.GetString(buffer);
                    ClientRequest retClientRequest = JsonConvert.DeserializeObject <ClientRequest>(ret);

                    serverResponse = new ServerResponse()
                    {
                        ResponseType       = "DownloadImage",
                        FeatureDescription = retClientRequest.FeatureDescription,
                        Image = retClientRequest.Image
                    };

                    string ret2 = JsonConvert.SerializeObject(serverResponse);
                    SendThis(ret2);
                    break;
                }
                break;
            }

            case RequestType.DeleteImage:
            {
                BlobConnector blob = new BlobConnector();
                blob.DeleteBlob("arimages", clientRequest.Name);

                ServerResponse serverResponse = new ServerResponse()
                {
                    ResponseType = "DeleteImage"
                };

                string ret = JsonConvert.SerializeObject(serverResponse);
                SendThis(ret);

                break;
            }

            case RequestType.DeleteAllImages:
            {
                BlobConnector blobConnector   = new BlobConnector();
                List <string> blobDirectories = new List <string>();
                blobDirectories = blobConnector.ListBlobs("arimages");
                foreach (string blob in blobDirectories)
                {
                    blobConnector.DeleteBlob("arimages", blob);
                }
            }
            break;

            case RequestType.ListBlobDirectories:
            {
                BlobConnector blobConnector   = new BlobConnector();
                List <string> blobDirectories = new List <string>();
                blobDirectories = blobConnector.ListBlobDirectories("arimages");

                ServerResponse serverResponse = new ServerResponse()
                {
                    ResponseType    = "ListBlobDirectories",
                    BlobDirectories = blobDirectories
                };

                string ret = JsonConvert.SerializeObject(serverResponse);
                SendThis(ret);

                break;
            }

            case RequestType.ListBlobsInDirectory:
            {
                BlobConnector blobConnector   = new BlobConnector();
                List <string> blobDirectories = new List <string>();
                blobDirectories = blobConnector.ListBlobs("arimages");

                ServerResponse serverResponse = new ServerResponse()
                {
                    ResponseType    = "ListBlobsInDirectory",
                    BlobDirectories = blobDirectories
                };

                string ret = JsonConvert.SerializeObject(serverResponse);
                SendThis(ret);

                break;
            }
            }
        }