コード例 #1
0
        /// <summary>
        ///     Gets the file e-tag.
        /// </summary>
        /// <param name="fileRef">The file reference.</param>
        /// <returns></returns>
        private string GetFileEtag(ERM.EntityRef fileRef)
        {
            var file = ERM.Entity.Get <ERM.FileType>(fileRef, ERM.FileType.FileDataHash_Field);

            // Get the entity tag, which is the hash of the image data
            string etag = string.Format("\"{0}\"", file.FileDataHash);

            return(etag);
        }
コード例 #2
0
        public HttpResponseMessage <string> Run(string idOrAlias, [FromBody] List <ParameterValue> values, bool trace = false)
        {
            using (Profiler.Measure("WorkflowController.Run"))
            {
                MDL.EntityRef eid = GetId(idOrAlias);

                var workflow = MDL.Entity.Get <MDL.Workflow>(eid);

                var parameterValues = new Dictionary <string, object>( );                // { { "ResourceId", new EntityRef(resourceId) } };

                using (new SecurityBypassContext())
                {
                    if (values != null)
                    {
                        foreach (ParameterValue kv in values)
                        {
                            EventLog.Application.WriteTrace("Running workflow {0} with {1}={2} ({3})", eid.Id, kv.Name,
                                                            kv.Value, kv.TypeName);

                            var argType = MDL.Entity.Get <MDL.ArgumentType>(kv.TypeName, MDL.ArgumentType.InternalDisplayName_Field);

                            switch (argType.Alias)
                            {
                            case "core:objectArgument":
                                throw new ApplicationException("Object arguments not implemented.");

                            case "core:resourceListArgument":
                                var ids = Jil.JSON.Deserialize <IEnumerable <long> >(kv.Value);
                                parameterValues.Add(kv.Name, Factory.EntityRepository.Get <MDL.IEntity>(ids));
                                break;

                            case "core:resourceArgument":
                                parameterValues.Add(kv.Name, GetId(kv.Value).Entity);
                                break;

                            default:
                                DatabaseType dbType = DatabaseType.ConvertFromDisplayName(argType.InternalDisplayName);
                                parameterValues.Add(kv.Name, dbType.ConvertFromString(kv.Value));
                                break;
                            }
                        }
                    }
                }

                var taskId = WorkflowRunner.Instance.RunWorkflowAsync(new WorkflowStartEvent(workflow)
                {
                    Arguments = parameterValues, Trace = trace
                });

                return(new HttpResponseMessage <string>(taskId));
            }
        }
コード例 #3
0
        public HttpResponseMessage <IEnumerable <string> > GetValidate(string idOrAlias)
        {
            using (Profiler.Measure("WorkflowController.GetValidate"))
            {
                MDL.EntityRef eid = GetId(idOrAlias);

                var workflow = MDL.Entity.Get <MDL.Workflow>(eid);

                IEnumerable <string> messages = workflow.Validate().Distinct();

                return(new HttpResponseMessage <IEnumerable <string> >(messages));
            }
        }
コード例 #4
0
        /// <summary>
        ///     Get a file for a request. If the
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="imageIdRef">The image identifier reference.</param>
        /// <param name="generateEtagFn">The generate e-tag function.</param>
        /// <returns>
        ///     The file response or a NotModified response if the e-tag matches.
        /// </returns>
        public static HttpResponseMessage GetFileForRequest(HttpRequestMessage request, ERM.EntityRef imageIdRef, Func <ERM.EntityRef, string> generateEtagFn)
        {
            string etag = generateEtagFn(imageIdRef);

            // If the request contains the same e-tag then return a not modified
            if (request.Headers.IfNoneMatch != null &&
                request.Headers.IfNoneMatch.Any(et => et.Tag == etag))
            {
                var notModifiedResponse = new HttpResponseMessage(HttpStatusCode.NotModified);
                notModifiedResponse.Headers.ETag = new EntityTagHeaderValue(etag, false);
                return(notModifiedResponse);
            }

            // Return the image
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(FileRepositoryHelper.GetFileDataStreamForEntity(imageIdRef))
            };

            response.Headers.ETag         = new EntityTagHeaderValue(etag, false);
            response.Headers.CacheControl = new CacheControlHeaderValue
            {
                MustRevalidate = true,
                MaxAge         = new TimeSpan(0)
            };

            // Yuck. You gotta do what you gotta do.
            bool isIosDevice = false;

            if (request.Headers.UserAgent != null)
            {
                isIosDevice = request.Headers.UserAgent.Any(ua => IosMobileRegex.IsMatch(ua.ToString()));
            }

            string contentType = string.Empty;

            // Note: We are not setting the content length because the CompressionHandler will compress the stream.
            // Specifying the length here will cause the browser to hang as the actual data it
            // receives (as it is compressed) will be less than the specified content length.
            FileDetails dbFileType = FileRepositoryHelper.GetStreamContentType(imageIdRef);

            if (dbFileType != null)
            {
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = dbFileType.Filename
                };

                // Found that specifying specific mime types on IOS mobile
                // causes safari to raise errors.
                // However, found that on Android the download
                // manager requires the mime type so that the downloaded
                // file can be opened by tapping.
                if (!isIosDevice)
                {
                    if (!string.IsNullOrWhiteSpace(dbFileType.ContentType))
                    {
                        contentType = dbFileType.ContentType;
                    }
                    else
                    {
                        // Attempt to get a mime type from the file name
                        if (!string.IsNullOrWhiteSpace(dbFileType.Filename))
                        {
                            contentType = MimeMapping.GetMimeMapping(dbFileType.Filename);
                        }
                    }
                }
            }

            // If we don't have a content type, fallback to the generic one.
            if (string.IsNullOrWhiteSpace(contentType))
            {
                contentType = "application/octet-stream";
            }

            response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

            return(response);
        }