コード例 #1
0
        public ActionResult Edit(HiveId?id)
        {
            if (id.IsNullValueOrEmpty())
            {
                return(HttpNotFound());
            }

            using (var uow = _hive.Create())
            {
                var def = PackageBuilderHelper.GetPackageDefinitionById(uow, id.Value);
                if (def == null)
                {
                    return(HttpNotFound());
                }

                var pkg = PackageBuilderHelper.GetPackageFileById(uow, id.Value);

                var model = BackOfficeRequestContext.Application.FrameworkContext.TypeMappers
                            .Map <PackageDefinition, PackageDefinitionEditorModel>(def);
                model.Id          = id.Value;
                model.IsPublished = pkg != null;

                PopulateCollections(model);

                return(View(model));
            }
        }
コード例 #2
0
        internal static IDictionary <string, object> WriteUploadedFile(Guid mediaId, bool removeExistingFile, HttpPostedFileBase httpFile, IGroupUnitFactory <IFileStore> groupUnitFactory, HiveId existingFileId = default(HiveId), string thumbSizes = null)
        {
            var val = new Dictionary <string, object>();

            //add the media id to be saved
            val.Add("MediaId", mediaId.ToString("N"));

            // Check to see if we should delete the current file
            // either becuase remove file is checked, or we have a replacement file
            if (existingFileId != HiveId.Empty && (removeExistingFile || HasFile(httpFile)))
            {
                if (!existingFileId.IsNullValueOrEmpty())
                {
                    // delete entire property folder (deletes image and any thumbnails stored)
                    //var folderHiveId = HiveId.Parse("storage://file-uploader/string/" + MediaId.ToString("N"));
                    var folderHiveId = new HiveId("storage", "file-uploader", new HiveIdValue(mediaId.ToString("N")));

                    using (var uow = groupUnitFactory.Create())
                    {
                        try
                        {
                            uow.Repositories.Delete <File>(existingFileId); // Must delete file entity so that relations are deleted
                            uow.Repositories.Delete <File>(folderHiveId);
                            uow.Complete();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Warn(typeof(ContentExtensions), "Could not delete previous file and/or container", ex);
                        }
                    }
                }
            }

            // If we've received a File from the binding, we need to save it
            if (HasFile(httpFile))
            {
                // Open a new unit of work to write the file
                using (var uow = groupUnitFactory.Create())
                {
                    // Create main file
                    var file = new File
                    {
                        RootedPath =
                            mediaId.ToString("N") + "/" + Path.GetFileName(httpFile.FileName)
                            .Replace(" ", "").Replace(",", "")
                    };

                    var stream = httpFile.InputStream;
                    if (stream.CanRead && stream.CanSeek)
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        using (var mem = new MemoryStream())
                        {
                            stream.CopyTo(mem);
                            file.ContentBytes = mem.ToArray();
                        }
                    }

                    uow.Repositories.AddOrUpdate(file);

                    // Create thumbnails (TODO: Need to encapsulate this so it can be reused in other places?)
                    CreateThumbnails(uow, file, mediaId.ToString("N"), thumbSizes);

                    uow.Complete();

                    val.Add("Value", file.Id);
                }
            }
            else if (!existingFileId.IsNullValueOrEmpty() && !removeExistingFile)
            {
                val.Add("Value", existingFileId);
            }
            else
            {
                val.Add("Value", HiveId.Empty);
            }

            return(val);
        }
コード例 #3
0
        internal static IDictionary<string, object> WriteUploadedFile(Guid mediaId, bool removeExistingFile, HttpPostedFileBase httpFile, IGroupUnitFactory<IFileStore> groupUnitFactory, HiveId existingFileId = default(HiveId), string thumbSizes = null)
        {
            var val = new Dictionary<string, object>();

            //add the media id to be saved
            val.Add("MediaId", mediaId.ToString("N"));

            // Check to see if we should delete the current file
            // either becuase remove file is checked, or we have a replacement file
            if (existingFileId != HiveId.Empty && (removeExistingFile || HasFile(httpFile)))
            {
                if (!existingFileId.IsNullValueOrEmpty())
                {
                    // delete entire property folder (deletes image and any thumbnails stored)
                    //var folderHiveId = HiveId.Parse("storage://file-uploader/string/" + MediaId.ToString("N"));
                    var folderHiveId = new HiveId("storage", "file-uploader", new HiveIdValue(mediaId.ToString("N")));

                    using (var uow = groupUnitFactory.Create())
                    {
                        try
                        {
                            uow.Repositories.Delete<File>(existingFileId); // Must delete file entity so that relations are deleted
                            uow.Repositories.Delete<File>(folderHiveId);
                            uow.Complete();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Warn(typeof(ContentExtensions), "Could not delete previous file and/or container", ex);
                        }
                    }
                }
            }

            // If we've received a File from the binding, we need to save it
            if (HasFile(httpFile))
            {
                // Open a new unit of work to write the file
                using (var uow = groupUnitFactory.Create())
                {
                    // Create main file
                    var file = new File
                                   {
                                       RootedPath =
                                           mediaId.ToString("N") + "/" + Path.GetFileName(httpFile.FileName)
                                                .Replace(" ", "").Replace(",", "")
                                   };

                    var stream = httpFile.InputStream;
                    if (stream.CanRead && stream.CanSeek)
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        using (var mem = new MemoryStream())
                        {
                            stream.CopyTo(mem);
                            file.ContentBytes = mem.ToArray();
                        }
                    }

                    uow.Repositories.AddOrUpdate(file);

                    // Create thumbnails (TODO: Need to encapsulate this so it can be reused in other places?)
                    CreateThumbnails(uow, file, mediaId.ToString("N"), thumbSizes);

                    uow.Complete();

                    val.Add("Value", file.Id);
                }
            }
            else if (!existingFileId.IsNullValueOrEmpty() && !removeExistingFile)
            {
                val.Add("Value", existingFileId);
            }
            else
            {
                val.Add("Value", HiveId.Empty);
            }

            return val;
        }
コード例 #4
0
        public JsonResult CreateSnapshot(SnapshotModel model)
        {
            var resultModel = new SnapshotResultModel();
            var snap        = DateTime.Now.Ticks.ToString();

            resultModel.SnapshotCreated  = true;
            resultModel.SnapshotLocation = "/App_Data/Data/" + snap;

            using (var uow = _hive.Create())
            {
                // Create folder
                var folder = new Rebel.Framework.Persistence.Model.IO.File(snap, "")
                {
                    IsContainer = true
                };
                uow.Repositories.AddOrUpdate(folder);
                uow.Complete();
            }

            var docTypeList = new Dictionary <string, string>();
            var contentList = new Dictionary <string, string>();
            var mediaList   = new Dictionary <string, string>();

            if (!model.IncludeDocumentTypes)
            {
                //Access ContentStore and get all distinct document types
                using (var uow = _requestContext.Application.Hive.OpenReader <IContentStore>())
                {
                    var docTypes =
                        uow.Repositories.Schemas.GetDescendentRelations(FixedHiveIds.ContentRootSchema,
                                                                        FixedRelationTypes.DefaultRelationType)
                        .Where(x => !x.DestinationId.IsSystem())
                        .DistinctBy(x => x.DestinationId);

                    foreach (var docType in docTypes)
                    {
                        var schema = uow.Repositories.Schemas.Get <EntitySchema>(docType.DestinationId);
                        var result = _requestContext.Application.FrameworkContext.Serialization.ToStream(schema);
                        docTypeList.Add(docType.DestinationId.Value.ToString(), result.ResultStream.ToJsonString());
                    }
                }

                //Dump json strings as files to 'DocumentType' data folder
                if (docTypeList.Any())
                {
                    using (var uow = _hive.Create())
                    {
                        foreach (var pair in docTypeList)
                        {
                            var file = new Rebel.Framework.Persistence.Model.IO.File(
                                snap + "/DocumentTypes/" + pair.Key,
                                Encoding.UTF8.GetBytes(pair.Value));
                            uow.Repositories.AddOrUpdate(file);
                        }

                        uow.Complete();
                    }
                }
            }

            if (!model.IncludeMedia)
            {
                //Access ContentStore and get all distinct media
                using (var uow = _requestContext.Application.Hive.OpenReader <IMediaStore>())
                {
                    var medias =
                        uow.Repositories.Schemas.GetDescendentRelations(FixedHiveIds.MediaVirtualRoot,
                                                                        FixedRelationTypes.DefaultRelationType)
                        .Where(x => !x.DestinationId.IsSystem())
                        .DistinctBy(x => x.DestinationId);

                    foreach (var media in medias)
                    {
                        var schema = uow.Repositories.Get(media.DestinationId);
                        var result = _requestContext.Application.FrameworkContext.Serialization.ToStream(schema);
                        mediaList.Add(media.DestinationId.Value.ToString(), result.ResultStream.ToJsonString());
                    }
                }

                //Dump json strings as files to 'Media' data folder
                if (mediaList.Any())
                {
                    using (var uow = _hive.Create())
                    {
                        foreach (var pair in docTypeList)
                        {
                            var file = new Rebel.Framework.Persistence.Model.IO.File(
                                snap + "/Media/" + pair.Key,
                                Encoding.UTF8.GetBytes(pair.Value));
                            uow.Repositories.AddOrUpdate(file);
                        }

                        uow.Complete();
                    }
                }
            }

            if (!model.IncludeContent)
            {
                //Access ContentStore and get all distinct content - latest revision
                using (var uow = _requestContext.Application.Hive.OpenReader <IContentStore>())
                {
                    var contents =
                        uow.Repositories.Schemas.GetDescendentRelations(FixedHiveIds.ContentVirtualRoot,
                                                                        FixedRelationTypes.DefaultRelationType)
                        .Where(x => !x.DestinationId.IsSystem())
                        .DistinctBy(x => x.DestinationId);

                    foreach (var content in contents)
                    {
                        var schema = uow.Repositories.Get(content.DestinationId);
                        var result = _requestContext.Application.FrameworkContext.Serialization.ToStream(schema);
                        contentList.Add(content.DestinationId.Value.ToString(), result.ResultStream.ToJsonString());
                    }
                }

                //Dump json strings as files to 'Content' data folder
                if (contentList.Any())
                {
                    using (var uow = _hive.Create())
                    {
                        foreach (var pair in docTypeList)
                        {
                            var file = new Rebel.Framework.Persistence.Model.IO.File(
                                snap + "/Content/" + pair.Key,
                                Encoding.UTF8.GetBytes(pair.Value));
                            uow.Repositories.AddOrUpdate(file);
                        }

                        uow.Complete();
                    }
                }
            }

            //Set Success
            resultModel.NotificationTitle   = "Snapshot created";
            resultModel.NotificationMessage = "A snapshot with the selected types has been created";
            resultModel.NotificationType    = NotificationType.Success.ToString().ToLower();

            //Invalid data in model (client slide validation should catch this, in as fail safe)

            /*resultModel.SnapshotCreated = false;
             *
             * resultModel.NotificationTitle = "An error occured";
             * resultModel.NotificationMessage = "Some of the data was invalid";
             * resultModel.NotificationType = NotificationType.Error.ToString().ToLower();*/

            //Return some JSON
            return(Json(resultModel));
        }