1
1
using System ;
2
2
using System . Globalization ;
3
3
using System . IO ;
4
+ using System . Linq ;
4
5
using System . Net ;
5
6
using System . Net . Mail ;
6
7
using System . Text ;
7
8
using System . Text . RegularExpressions ;
8
9
using System . Web ;
9
10
using System . Web . UI ;
10
11
using System . Xml ;
12
+ using System . Xml . Linq ;
11
13
using System . Xml . XPath ;
14
+ using Newtonsoft . Json ;
12
15
using Umbraco . Core ;
13
16
using Umbraco . Core . Cache ;
17
+ using Umbraco . Core . Configuration ;
14
18
using Umbraco . Core . Logging ;
19
+ using Umbraco . Core . Models ;
20
+ using Umbraco . Core . Services ;
15
21
using Umbraco . Web ;
16
22
using Umbraco . Web . Cache ;
17
- using Umbraco . Web . PublishedCache ;
18
- using Umbraco . Web . Routing ;
19
23
using Umbraco . Web . Templates ;
20
24
using umbraco . BusinessLogic ;
21
25
using umbraco . cms . businesslogic ;
22
- using umbraco . cms . businesslogic . media ;
23
- using umbraco . cms . businesslogic . member ;
24
- using umbraco . cms . businesslogic . propertytype ;
25
- using umbraco . cms . businesslogic . relation ;
26
26
using umbraco . cms . businesslogic . web ;
27
27
using umbraco . cms . helpers ;
28
- using umbraco . presentation . cache ;
29
28
using umbraco . scripting ;
30
29
using umbraco . DataLayer ;
31
- using System . Web . Security ;
32
- using umbraco . cms . businesslogic . language ;
33
30
using Umbraco . Core . IO ;
34
- using System . Collections ;
35
- using System . Collections . Generic ;
36
- using umbraco . cms . businesslogic . cache ;
37
- using umbraco . NodeFactory ;
31
+ using Language = umbraco . cms . businesslogic . language . Language ;
32
+ using Media = umbraco . cms . businesslogic . media . Media ;
33
+ using Member = umbraco . cms . businesslogic . member . Member ;
34
+ using PropertyType = umbraco . cms . businesslogic . propertytype . PropertyType ;
35
+ using Relation = umbraco . cms . businesslogic . relation . Relation ;
38
36
using UmbracoContext = umbraco . presentation . UmbracoContext ;
39
- using System . Linq ;
40
37
41
38
namespace umbraco
42
39
{
@@ -470,46 +467,48 @@ public static XPathNodeIterator GetMedia(int MediaId, bool Deep)
470
467
{
471
468
if ( UmbracoSettings . UmbracoLibraryCacheDuration > 0 )
472
469
{
473
- XPathNodeIterator retVal = ApplicationContext . Current . ApplicationCache . GetCacheItem (
470
+ var xml = ApplicationContext . Current . ApplicationCache . GetCacheItem (
474
471
string . Format (
475
472
"{0}_{1}_{2}" , CacheKeys . MediaCacheKey , MediaId , Deep ) ,
476
473
TimeSpan . FromSeconds ( UmbracoSettings . UmbracoLibraryCacheDuration ) ,
477
474
( ) => GetMediaDo ( MediaId , Deep ) ) ;
478
475
479
- if ( retVal != null )
480
- return retVal ;
476
+ if ( xml != null )
477
+ {
478
+ //returning the root element of the Media item fixes the problem
479
+ return xml . CreateNavigator ( ) . Select ( "/" ) ;
480
+ }
481
+
481
482
}
482
483
else
483
484
{
484
- return GetMediaDo ( MediaId , Deep ) ;
485
+ var xml = GetMediaDo ( MediaId , Deep ) ;
486
+
487
+ //returning the root element of the Media item fixes the problem
488
+ return xml . CreateNavigator ( ) . Select ( "/" ) ;
485
489
}
486
-
487
490
}
488
- catch
491
+ catch ( Exception ex )
489
492
{
493
+ LogHelper . Error < library > ( "An error occurred looking up media" , ex ) ;
490
494
}
491
495
492
- XmlDocument xd = new XmlDocument ( ) ;
496
+ LogHelper . Debug < library > ( "No media result for id {0}" , ( ) => MediaId ) ;
497
+
498
+ var xd = new XmlDocument ( ) ;
493
499
xd . LoadXml ( string . Format ( "<error>No media is maching '{0}'</error>" , MediaId ) ) ;
494
500
return xd . CreateNavigator ( ) . Select ( "/" ) ;
495
501
}
496
502
497
- private static XPathNodeIterator GetMediaDo ( int mediaId , bool deep )
503
+ private static XElement GetMediaDo ( int mediaId , bool deep )
498
504
{
499
- var m = new Media ( mediaId ) ;
500
- if ( m . nodeObjectType == Media . _objectType )
501
- {
502
- var mXml = new XmlDocument ( ) ;
503
- var xml = m . ToXml ( mXml , deep ) ;
504
- //This will be null if the media isn't public (meaning it is in the trash)
505
- if ( xml == null ) return null ;
506
- //TODO: This is an aweful way of loading in XML - it is very slow.
507
- mXml . LoadXml ( xml . OuterXml ) ;
508
- var xp = mXml . CreateNavigator ( ) ;
509
- var xpath = UmbracoSettings . UseLegacyXmlSchema ? "/node" : String . Format ( "/{0}" , Casing . SafeAliasWithForcingCheck ( m . ContentType . Alias ) ) ;
510
- return xp . Select ( xpath ) ;
511
- }
512
- return null ;
505
+ var media = ApplicationContext . Current . Services . MediaService . GetById ( mediaId ) ;
506
+ if ( media == null ) return null ;
507
+ var serializer = new EntityXmlSerializer ( ) ;
508
+ var serialized = serializer . Serialize (
509
+ ApplicationContext . Current . Services . MediaService , ApplicationContext . Current . Services . DataTypeService ,
510
+ media , deep ) ;
511
+ return serialized ;
513
512
}
514
513
515
514
/// <summary>
@@ -525,35 +524,42 @@ public static XPathNodeIterator GetMember(int MemberId)
525
524
{
526
525
if ( UmbracoSettings . UmbracoLibraryCacheDuration > 0 )
527
526
{
528
- var retVal = ApplicationContext . Current . ApplicationCache . GetCacheItem (
527
+ var xml = ApplicationContext . Current . ApplicationCache . GetCacheItem (
529
528
string . Format (
530
529
"{0}_{1}" , CacheKeys . MemberLibraryCacheKey , MemberId ) ,
531
530
TimeSpan . FromSeconds ( UmbracoSettings . UmbracoLibraryCacheDuration ) ,
532
531
( ) => GetMemberDo ( MemberId ) ) ;
533
532
534
- if ( retVal != null )
535
- return retVal . CreateNavigator ( ) . Select ( "/" ) ;
533
+ if ( xml != null )
534
+ {
535
+ return xml . CreateNavigator ( ) . Select ( "/" ) ;
536
+ }
536
537
}
537
538
else
538
539
{
539
540
return GetMemberDo ( MemberId ) . CreateNavigator ( ) . Select ( "/" ) ;
540
541
}
541
-
542
542
}
543
- catch
543
+ catch ( Exception ex )
544
544
{
545
+ LogHelper . Error < library > ( "An error occurred looking up member" , ex ) ;
545
546
}
546
- XmlDocument xd = new XmlDocument ( ) ;
547
+
548
+ LogHelper . Debug < library > ( "No member result for id {0}" , ( ) => MemberId ) ;
549
+
550
+ var xd = new XmlDocument ( ) ;
547
551
xd . LoadXml ( string . Format ( "<error>No member is maching '{0}'</error>" , MemberId ) ) ;
548
552
return xd . CreateNavigator ( ) . Select ( "/" ) ;
549
553
}
550
554
551
- private static XmlDocument GetMemberDo ( int MemberId )
555
+ private static XElement GetMemberDo ( int MemberId )
552
556
{
553
- Member m = new Member ( MemberId ) ;
554
- XmlDocument mXml = new XmlDocument ( ) ;
555
- mXml . LoadXml ( m . ToXml ( mXml , false ) . OuterXml ) ;
556
- return mXml ;
557
+ var member = ApplicationContext . Current . Services . MemberService . GetById ( MemberId ) ;
558
+ if ( member == null ) return null ;
559
+ var serializer = new EntityXmlSerializer ( ) ;
560
+ var serialized = serializer . Serialize (
561
+ ApplicationContext . Current . Services . DataTypeService , member ) ;
562
+ return serialized ;
557
563
}
558
564
559
565
/// <summary>
@@ -1352,8 +1358,9 @@ public static XPathNodeIterator GetXmlNodeCurrent()
1352
1358
nav . MoveToId ( HttpContext . Current . Items [ "pageID" ] . ToString ( ) ) ;
1353
1359
return nav . Select ( "." ) ;
1354
1360
}
1355
- catch
1361
+ catch ( Exception ex )
1356
1362
{
1363
+ LogHelper . Error < library > ( "Could not retrieve current xml node" , ex ) ;
1357
1364
}
1358
1365
1359
1366
XmlDocument xd = new XmlDocument ( ) ;
0 commit comments