public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "list/{groupStr}")] HttpRequestMessage req, TraceWriter log, string groupStr)
        {
            List <DocumentTaskEntity> documents;

            try
            {
                documents = await DatabaseConnection.db.ListGroup(groupStr);
            } catch
            {
                JObject errResp = new JObject();
                errResp["group"] = groupStr;
                errResp["error"] = "internal_server_error";
                return(HttpCommon.GenerateJsonResponse(HttpStatusCode.InternalServerError, errResp));
            }

            JArray arr = new JArray();

            foreach (DocumentTaskEntity x in documents)
            {
                JObject documentJson = new JObject();
                documentJson["id"]     = x.DocumentId.ToString();
                documentJson["status"] = x.State;
                arr.Add(documentJson);
            }

            JObject responseJson = new JObject();

            responseJson["group"] = groupStr;
            responseJson["tasks"] = arr;
            return(HttpCommon.GenerateJsonResponse(HttpStatusCode.OK, responseJson));
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "status/{guidStr}")] HttpRequestMessage req, TraceWriter log, string guidStr)
        {
            Guid guid;

            try
            {
                guid = new Guid(guidStr);
            }
            catch
            {
                return(HttpCommon.GenerateJsonError(HttpStatusCode.BadRequest, "invalid_guid"));
            }

            JObject            responseJson = new JObject();
            DocumentTaskEntity document;

            try
            {
                document = await DatabaseConnection.db.GetTask(guid);
            }
            catch (InvalidOperationException e)
            {
                return(HttpCommon.GenerateJsonError(HttpStatusCode.NotFound, guid, "not_found"));
            }
            JArray outputList = new JArray();

            if (document.ResultFiles != null)
            {
                foreach (BatchBlobUrlInput x in document.ResultFiles)
                {
                    JObject obj = new JObject();
                    obj["part"] = x.Part;
                    obj["url"]  = x.Url;
                    outputList.Add(obj);
                }
            }
            responseJson["id"]     = guid.ToString();
            responseJson["status"] = document.State;
            if (document.State == "complete")
            {
                responseJson["output"] = outputList;
            }
            return(HttpCommon.GenerateJsonResponse(HttpStatusCode.OK, responseJson));
        }
Exemple #3
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "cancel/{guidStr}")] HttpRequestMessage req, TraceWriter log, string guidStr)
        {
            Guid guid;

            try
            {
                guid = new Guid(guidStr);
            } catch
            {
                return(HttpCommon.GenerateJsonError(HttpStatusCode.BadRequest, "invalid_guid"));
            }

            JObject responseJson = new JObject();

            try
            {
                await DatabaseConnection.db.CancelDocument(guid);
            } catch (InvalidOperationException e)
            {
                switch (e.Message)
                {
                case "not_found":
                    return(HttpCommon.GenerateJsonError(HttpStatusCode.NotFound, guid, "not_found"));

                case "update_failed":
                    return(HttpCommon.GenerateJsonError(HttpStatusCode.Gone, guid, "update_failed"));

                case "wrong_state":
                    return(HttpCommon.GenerateJsonError(HttpStatusCode.BadRequest, guid, "wrong_state"));;

                default:
                    return(HttpCommon.GenerateJsonError(HttpStatusCode.InternalServerError, guid, "internal_server_error"));
                }
            }
            responseJson["id"]     = guid.ToString();
            responseJson["status"] = "cancelling";
            return(HttpCommon.GenerateJsonResponse(HttpStatusCode.OK, responseJson));
        }
Exemple #4
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "create")] HttpRequestMessage req, TraceWriter log)
        {
            JObject input;
            string  group    = null;
            string  callback = null;
            List <UrlUploadFile>    urlUploadFiles = new List <UrlUploadFile>();
            List <UploadUploadFile> uploadFiles    = new List <UploadUploadFile>();
            List <InlineUploadFile> inlineFiles    = new List <InlineUploadFile>();

            // Read request
            try
            {
                input = await HttpCommon.ReadRequestJsonObject(req);

                if (input["group"] != null)
                {
                    group = (string)input["group"];
                }
                if (input["callback"] != null)
                {
                    callback = (string)input["callback"];
                }
                JArray inputFilesJArray = (JArray)input["document"];
                foreach (JToken x in inputFilesJArray)
                {
                    JObject inFileObj = (JObject)x;
                    switch ((string)x["type"])
                    {
                    case "inline":
                    {
                        // TODO: Add to list of files to upload immediately
                        string           body   = (string)x["body"];
                        string           part   = (string)x["part"];
                        InlineUploadFile inline = new InlineUploadFile(part, body);
                        inlineFiles.Add(inline);
                    }
                    break;

                    case "upload":
                    {
                        // TODO: Get URL / SAS for provided GUID
                        string           id     = (string)x["id"];
                        Guid             uid    = new Guid(id);
                        string           part   = (string)x["part"];
                        UploadUploadFile upload = new UploadUploadFile(part, uid);
                        uploadFiles.Add(upload);
                    }
                    break;

                    case "url":
                    {
                        // TODO: Check if url is in blob storage
                        string        url     = (string)x["url"];
                        string        part    = (string)x["part"];
                        UrlUploadFile urlFile = new UrlUploadFile(part, url);
                        urlUploadFiles.Add(urlFile);
                    }
                    break;

                    default:
                        return(HttpCommon.GenerateJsonError(HttpStatusCode.BadRequest, "bad_request"));
                        //break;
                    }
                }
            }
            catch
            {
                return(HttpCommon.GenerateJsonError(HttpStatusCode.BadRequest, "bad_request"));
            }

            List <BatchBlobUrlInput> inputFileSasUrls = new List <BatchBlobUrlInput>();

            // Upload inline files
            foreach (InlineUploadFile x in inlineFiles)
            {
                Guid inlineGuid = await DatabaseConnection.blx.UploadInlineFile(x.Body);

                string            inlineFileSasUrl = DatabaseConnection.blx.GetFileSasReadUrl(inlineGuid);
                BatchBlobUrlInput inlineUrlInput   = new BatchBlobUrlInput(x.Part, inlineFileSasUrl);
                inputFileSasUrls.Add(inlineUrlInput);
            }

            // Also need to create specification file with list of input files

            // Assume for now that all URLs already point to blob storage
            foreach (UrlUploadFile x in urlUploadFiles)
            {
                BatchBlobUrlInput inx = new BatchBlobUrlInput(x);
                inputFileSasUrls.Add(inx);
            }
            urlUploadFiles = new List <UrlUploadFile>();

            // List of URLs from a combination of inline, file, and blob storage uploads
            // Non-blob-storage URLs (urlUploadFiles) will have to be added later

            // Perform operation and generate response
            try
            {
                JObject responseJson = new JObject();
                Guid    guid         = await DatabaseConnection.db.AddNewDocument(group, callback, urlUploadFiles, inputFileSasUrls);

                responseJson["id"]     = guid.ToString();
                responseJson["status"] = "creating";
                return(HttpCommon.GenerateJsonResponse(HttpStatusCode.OK, responseJson));
            }
            catch (InvalidOperationException e)
            {
                return(HttpCommon.GenerateJsonError(HttpStatusCode.InternalServerError, "internal_server_error"));
            }
        }