예제 #1
0
        internal Yield PutSiteLogo(DreamContext context, DreamMessage request, Result<DreamMessage> response) {

            //Confirm image type
            if(!new MimeType("image/*").Match(request.ContentType)) {
                throw new DreamAbortException(DreamMessage.BadRequest(DekiResources.EXPECTED_IMAGE_MIMETYPE));
            }
            try {
                //Save file to storage provider
                DekiContext.Current.Instance.Storage.PutSiteFile(LOGO_LABEL, new StreamInfo(request.AsStream(), request.ContentLength, request.ContentType));
                ConfigBL.SetInstanceSettingsValue(ConfigBL.UI_LOGO_UPLOADED, "true");
            } catch(Exception x) {
                DekiContext.Current.Instance.Log.Warn("Failed to save logo to storage provider", x);
                ConfigBL.DeleteInstanceSettingsValue(ConfigBL.UI_LOGO_UPLOADED);
                throw;
            }

            StreamInfo file = DekiContext.Current.Instance.Storage.GetSiteFile(LOGO_LABEL, false);
            if(file != null) {
                StreamInfo thumb = AttachmentPreviewBL.BuildThumb(file, FormatType.PNG, RatioType.UNDEFINED, DekiContext.Current.Instance.LogoWidth, DekiContext.Current.Instance.LogoHeight);
                if(thumb != null) {
                    DekiContext.Current.Instance.Storage.PutSiteFile(LOGO_LABEL, thumb);
                } else {
                    DekiContext.Current.Instance.Log.WarnMethodCall("PUT:site/logo", "Unable to process logo through imagemagick");
                    DekiContext.Current.ApiPlug.At("site", "logo").Delete();
                    throw new DreamBadRequestException(DekiResources.CANNOT_PROCESS_LOGO_IMAGE);
                }
            } else {
                DekiContext.Current.Instance.Log.WarnMethodCall("PUT:site/logo", "Unable to retrieve saved logo");
            }
            response.Return(DreamMessage.Ok());
            yield break;
        }
예제 #2
0
        public Yield PutFile(DreamContext context, DreamMessage request, Result<DreamMessage> response) {

            // Retrieve the file
            PageBE page;
            string userFileName;
            AttachmentBE file = GetAttachmentFromUrl(false, out page, false, false);
            
            // If the file does not exist, attempt to retrieve the page
            if (null == file) {
                if (null == page) {
                    if (null != DreamContext.Current.GetParam<string>("fileid")) {
                        throw new DreamAbortException(DreamMessage.NotFound(DekiResources.COULD_NOT_FIND_FILE));
                    }
                    page = PageBL.GetPageFromUrl();
                }
                userFileName = GetFilenameFromPathSegment(DreamContext.Current.GetParam<string>("filename"));
            } else {
                string fileNameParam = DreamContext.Current.GetParam("filename", null);
                if (fileNameParam == null) {
                    userFileName = file.Name;
                } else {
                    userFileName = GetFilenameFromPathSegment(fileNameParam);
                }
            }

            // Retrieve the file description
            string userDescription = context.GetParam("description", string.Empty);
            if(userDescription.Length > AttachmentBL.MAX_DESCRIPTION_LENGTH) {
                userDescription = userDescription.Substring(0, AttachmentBL.MAX_DESCRIPTION_LENGTH);
            }

            // Validate the page
            PageBL.AuthorizePage(DekiContext.Current.User, Permissions.UPDATE, page, false);

            // Get entire stream so it can be reused
            AttachmentBE savedFileRevision = AttachmentBL.Instance.AddAttachment(file, request.AsStream(), request.ContentLength, request.ContentType, page, userDescription, userFileName);

            // report an error on failure, and don't redirect
            if (savedFileRevision == null) {
                response.Return(DreamMessage.InternalError(DekiResources.FAILED_TO_SAVE_UPLOAD));
                yield break;
            }
            response.Return(DreamMessage.Ok(AttachmentBL.Instance.GetFileXml(savedFileRevision, true, null, null)));
            yield break;
        }
예제 #3
0
 public ResourceContentBE CreateDbSerializedContentFromRequest(DreamMessage request) {
     return CreateDbSerializedContentFromStream(request.AsStream(), request.ContentLength, request.ContentType);
 }
        private Yield PutResourcePropertyHelper(DreamContext context, DreamMessage request, Result<DreamMessage> response) {
            uint? parentId;
            ResourceBE.Type parentType;
            XUri parentUri;

            string etag = request.Headers.ETag ?? context.GetParam("etag", null);
            string description = context.GetParam("description", string.Empty);
            AbortEnum abort = SysUtil.ChangeType<AbortEnum>(context.GetParam("abort", "modified"));
            string name;

            PropertyBE prop = GetPropertyFromRequest(context, Permissions.UPDATE, false, false, out name, out parentId, out parentType, out parentUri);
            if(prop == null) {

                // abort if the user specified to abort on 'modified' and provided an 'etag'; since no etag exists, they are not the same
                if((abort == AbortEnum.Modified) && !string.IsNullOrEmpty(etag)) {
                    throw new DreamAbortException(DreamMessage.Conflict(DekiResources.PROPERTY_EDIT_NONEXISTING_CONFLICT));
                }

                // create the resource
                ResourceBE parentResource;
                PageBE parentPage;
                AuthorizeParent(context, Permissions.UPDATE, out parentId, out parentType, out parentUri, out parentPage, out parentResource);
                ResourceContentBE resourceContent = PropertyBL.Instance.CreateDbSerializedContentFromStream(request.AsStream(), request.ContentLength, request.ContentType);
                prop = PropertyBL.Instance.Create(parentId, parentUri, parentType, parentResource, name, resourceContent, description, etag, abort);
            } else if(abort == AbortEnum.Exists) {
                throw new DreamAbortException(DreamMessage.Conflict(string.Format(DekiResources.PROPERTY_EXISTS_CONFLICT, name)));
            } else {
                ResourceContentBE resourceContent = PropertyBL.Instance.CreateDbSerializedContentFromStream(request.AsStream(), request.ContentLength, request.ContentType);
                prop = PropertyBL.Instance.UpdateContent(prop, resourceContent, description, etag, abort, parentUri, parentType);
            }
            XDoc ret = PropertyBL.Instance.GetPropertyXml(prop, parentUri, null, null);
            response.Return(DreamMessage.Ok(ret));
            yield break;
        }
        private Yield PostResourcePropertyHelper(DreamContext context, DreamMessage request, Result<DreamMessage> response) {
            uint? parentId;
            ResourceBE.Type parentType;
            XUri parentUri;
            ResourceBE parentResource;
            PageBE parentPage;
            AuthorizeParent(context, Permissions.UPDATE, out parentId, out parentType, out parentUri, out parentPage, out parentResource);

            string etag = request.Headers.ETag ?? context.GetParam("etag", null);
            AbortEnum abort = SysUtil.ChangeType<AbortEnum>(context.GetParam("abort", "exists"));
            string description = context.GetParam("description", string.Empty);
            string name = XUri.Decode(request.Headers[SLUGHEADER]);
            if(string.IsNullOrEmpty(name)) {
                throw new DreamBadRequestException(DekiResources.PROPERTY_CREATE_MISSING_SLUG);
            }


            ResourceContentBE resourceContent = PropertyBL.Instance.CreateDbSerializedContentFromStream(request.AsStream(), request.ContentLength, request.ContentType);
            PropertyBE property = PropertyBL.Instance.Create(parentId, parentUri, parentType, parentResource, name, resourceContent, description, etag, abort);
            XDoc ret = PropertyBL.Instance.GetPropertyXml(property, parentUri, null, null);
            response.Return(DreamMessage.Ok(ret));
            yield break;
        }
예제 #6
0
        public Yield PutFile(DreamContext context, DreamMessage request, Result<DreamMessage> response) {

            // Retrieve the file
            PageBE page;
            string userFileName;
            ResourceBE file = GetAttachmentFromUrl(context, false, out page, false, false);

            // If the file does not exist, attempt to retrieve the page
            if(null == file) {
                if(null == page) {
                    if(null != DreamContext.Current.GetParam<string>("fileid")) {
                        throw new AttachmentNotFoundException();
                    }
                    page = PageBL_GetPageFromUrl(context, true);
                }
                userFileName = GetFilenameFromPathSegment(DreamContext.Current.GetParam<string>("filename"));
            } else {
                string fileNameParam = DreamContext.Current.GetParam("filename", null);
                if(fileNameParam == null) {
                    userFileName = file.Name;
                } else {
                    userFileName = GetFilenameFromPathSegment(fileNameParam);
                }
            }

            // Retrieve the file description
            string userDescription = context.GetParam("description", string.Empty);
            if(userDescription.Length > AttachmentBL.MAX_DESCRIPTION_LENGTH) {
                userDescription = userDescription.Substring(0, AttachmentBL.MAX_DESCRIPTION_LENGTH);
            }

            // Validate the page
            PageBL.AuthorizePage(DekiContext.Current.User, Permissions.UPDATE, page, false);

            // Get entire stream so it can be reused
            var isMSWebDAV = MSWEBDAV_USER_AGENT_REGEX.IsMatch(request.Headers.UserAgent ?? string.Empty);
            ResourceBE savedFileRevision = AttachmentBL.Instance.AddAttachment(file, request.AsStream(), request.ContentLength, request.ContentType, page, userDescription, userFileName, isMSWebDAV);

            // report an error on failure, and don't redirect
            if(savedFileRevision == null) {
                throw new AttachmentUploadSaveFatalException();
            }
            response.Return(DreamMessage.Ok(AttachmentBL.Instance.GetFileXml(savedFileRevision, true, null, null)));
            yield break;
        }