Example #1
0
        public HttpResponseMessage List(RequestDTO req)
        {
            try
            {
                Log.Logger.DebugFormat("OpenFiles.JplistApiController.List() called with request [{0}].", req.ToJson());

                FieldConfig  indexConfig  = FilesRepository.GetIndexConfig(PortalSettings.PortalId);
                bool         addWorkFlow  = PortalSettings.UserMode != PortalSettings.Mode.Edit;
                QueryBuilder queryBuilder = new QueryBuilder(indexConfig);
                queryBuilder.BuildFilter(PortalSettings.PortalId, req.folder, addWorkFlow, PortalSettings.UserInfo.Social.Roles);
                JplistQueryBuilder.MergeJpListQuery(FilesRepository.GetIndexConfig(PortalSettings), queryBuilder.Select, req.StatusLst, DnnLanguageUtils.GetCurrentCultureCode());

                string curFolder = NormalizePath(req.folder);
                foreach (var item in queryBuilder.Select.Query.FilterRules.Where(f => f.Field == LuceneMappingUtils.FolderField))
                {
                    curFolder  = NormalizePath(item.Value.AsString);
                    item.Value = new StringRuleValue(NormalizePath(item.Value.AsString)); //any file of current folder
                }

                var def = new SelectQueryDefinition();
                def.Build(queryBuilder.Select);

                var docs  = LuceneController.Instance.Search(def);
                int total = docs.TotalResults;



                var ratio = string.IsNullOrEmpty(req.imageRatio) ? new Ratio(100, 100) : new Ratio(req.imageRatio);

                Log.Logger.DebugFormat("OpenFiles.JplistApiController.List() Searched for [{0}], found [{1}] items", def.Filter.ToString() + " / " + def.Query.ToString(), total);

                //if (LogContext.IsLogActive)
                //{
                //    var logKey = "Query";
                //    LogContext.Log(ActiveModule.ModuleID, logKey, "select", queryBuilder.Select);
                //    LogContext.Log(ActiveModule.ModuleID, logKey, "debuginfo", dsItems.DebugInfo);
                //    LogContext.Log(ActiveModule.ModuleID, logKey, "model", model);
                //    model["Logs"] = JToken.FromObject(LogContext.Current.ModuleLogs(ActiveModule.ModuleID));
                //}

                var fileManager = FileManager.Instance;
                var data        = new List <FileDTO>();
                var breadcrumbs = new List <IFolderInfo>();
                if (req.withSubFolder)
                {
                    //hier blijken we resultaten toe te voegen die niet uit lucene komen
                    breadcrumbs = AddFolders(NormalizePath(req.folder), curFolder, fileManager, data, ratio);
                }

                foreach (var doc in docs.ids)
                {
                    IFileInfo f = fileManager.GetFile(doc.FileId);
                    if (f == null)
                    {
                        //file seems to have been deleted
                        LuceneController.Instance.Delete(LuceneMappingUtils.CreateLuceneItem(doc.PortalId, doc.FileId));
                        total -= 1;
                    }
                    else
                    {
                        if (f.FileName == "_folder.jpg")
                        {
                            continue; // skip
                        }
                        dynamic title  = null;
                        var     custom = GetCustomFileDataAsDynamic(f);
                        if (custom != null && custom.meta != null)
                        {
                            try
                            {
                                title = Normalize.DynamicValue(custom.meta.title, "");
                            }
                            catch (Exception ex)
                            {
                                Log.Logger.Debug("OpenFiles.JplistApiController.List() Failed to get title.", ex);
                            }
                        }

                        data.Add(new FileDTO()
                        {
                            Id                 = f.FileId,
                            Name               = Normalize.DynamicValue(title, f.FileName),
                            FileName           = f.FileName,
                            CreatedOnDate      = f.CreatedOnDate,
                            LastModifiedOnDate = f.LastModifiedOnDate,
                            FolderName         = f.Folder,
                            Url                = fileManager.GetUrl(f),
                            IsImage            = fileManager.IsImageFile(f),
                            ImageUrl           = ImageHelper.GetImageUrl(f, ratio),
                            Custom             = custom,
                            IconUrl            = GetFileIconUrl(f.Extension),
                            IsEditable         = IsEditable,
                            EditUrl            = IsEditable ? GetFileEditUrl(f) : ""
                        });
                    }
                }

                var res = new ResultExtDTO <FileDTO>()
                {
                    data = new ResultDataDTO <FileDTO>()
                    {
                        items       = data,
                        breadcrumbs = breadcrumbs.Select(f => new ResultBreadcrumbDTO
                        {
                            name = f.FolderName,
                            path = f.FolderPath.Trim('/')
                        })
                    },
                    count = total
                };
                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }
        public HttpResponseMessage List(RequestDTO req)
        {
            try
            {
                IEnumerable<LuceneIndexItem> docs;

                var jpListQuery = BuildJpListQuery(req.StatusLst);
                if (!string.IsNullOrEmpty(req.folder) && jpListQuery.Filters.All(f => f.name != "Folder")) // If there is no "Folder" filter active, then add one
                {
                    jpListQuery.Filters.Add(new FilterDTO()
                    {
                        name = "Folder",
                        value = NormalizePath(req.folder)
                    });
                }
                else
                {
                    foreach (var item in jpListQuery.Filters.Where(f => f.name == "Folder"))
                    {
                        item.path = NormalizePath(item.path);
                    }
                }
                jpListQuery.Filters.Add(new FilterDTO()
                {
                    name = "PortalId",
                    path = PortalSettings.PortalId.ToString()
                });

                string luceneQuery = BuildLuceneQuery(jpListQuery);
                if (string.IsNullOrEmpty(luceneQuery))
                {
                    docs = SearchEngine.GetAllIndexedRecords();
                }
                else
                {
                    docs = SearchEngine.Search(luceneQuery);
                }

                int total = docs.Count();
                if (jpListQuery.Pagination.currentPage > 0)
                    docs = docs.Skip(jpListQuery.Pagination.currentPage * jpListQuery.Pagination.number).Take(jpListQuery.Pagination.number);
                var fileManager = FileManager.Instance;
                var data = new List<FileDTO>();
                foreach (var doc in docs)
                {
                    var f = fileManager.GetFile(doc.FileId);
                    if (f == null)
                    {
                        //file seems to have been deleted
                        SearchEngine.RemoveDocument(doc.FileId);
                        total -= 1;
                    }
                    else
                    {
                        data.Add(new FileDTO()
                        {
                            FileName = f.FileName,
                            FolderName = f.Folder,
                            Url = fileManager.GetUrl(f),
                            ImageUrl = ImageHelper.GetImageUrl(f, new Ratio(100, 100)),
                            Custom = GetCustomFileDataAsDynamic(f),
                            IsImage = fileManager.IsImageFile(f),
                            IconUrl = GetFileIconUrl(f.Extension)
                        });
                    }
                }

                var res = new ResultDTO<FileDTO>()
                {
                    data = data,
                    count = total
                };

                return Request.CreateResponse(HttpStatusCode.OK, res);
            }
            catch (Exception exc)
            {
                Utils.Logger.Error(exc);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
            }
        }