Advertisement
andruhovski

Untitled

Oct 28th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.70 KB | None | 0 0
  1. using System.Web.Http;
  2. using System.Threading.Tasks;
  3. using Aspose.App.Models;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System;
  7. using System.Reflection;
  8. using Aspose.Cells.Properties;
  9. using Aspose.Pdf;
  10. using Aspose.Slides;
  11. using Tools.Foundation.Models;
  12. using Aspose.Email.Mapi;
  13. using System.Text;
  14. using Aspose.App.LibraryHelpers;
  15. using System.Linq;
  16.  
  17. namespace Aspose.App.Controllers
  18. {
  19. ///<Summary>
  20. /// AsposeMetadataController class to get metadata properties
  21. ///</Summary>
  22. public class AsposeMetadataController : ApiControllerBase
  23. {
  24. private List<string> allowedTypes = new List<string> { "Boolean", "DateTime", "Double", "Number", "Int32", "String" };
  25. private Boolean IsAllowedType(string propertyType)
  26. {
  27. return allowedTypes.Contains(propertyType);
  28. }
  29.  
  30. private void AddWordsCustomPropertyValue(Words.Properties.CustomDocumentProperties customDocumentProperties, AsposeMetadataObject metadataObj)
  31. {
  32. if (metadataObj.ValueType == "Boolean")
  33. {
  34. customDocumentProperties.Add(metadataObj.Property, Boolean.Parse(metadataObj.Value));
  35. }
  36. else if (metadataObj.ValueType == "DateTime")
  37. {
  38. customDocumentProperties.Add(metadataObj.Property, DateTime.Parse(metadataObj.Value));
  39. }
  40. else if (metadataObj.ValueType == "Double")
  41. {
  42. customDocumentProperties.Add(metadataObj.Property, Double.Parse(metadataObj.Value));
  43. }
  44. else if (metadataObj.ValueType == "Number" || metadataObj.ValueType == "Int32")
  45. {
  46. customDocumentProperties.Add(metadataObj.Property, Int32.Parse(metadataObj.Value));
  47. }
  48. else if (metadataObj.ValueType == "String")
  49. {
  50. customDocumentProperties.Add(metadataObj.Property, metadataObj.Value);
  51. }
  52. }
  53.  
  54. private void AddCellsCustomPropertyValue(CustomDocumentPropertyCollection customDocumentProperties, AsposeMetadataObject metadataObj)
  55. {
  56. if (metadataObj.ValueType == "Boolean")
  57. {
  58. customDocumentProperties.Add(metadataObj.Property, Boolean.Parse(metadataObj.Value));
  59. }
  60. else if (metadataObj.ValueType == "DateTime")
  61. {
  62. customDocumentProperties.Add(metadataObj.Property, DateTime.Parse(metadataObj.Value));
  63. }
  64. else if (metadataObj.ValueType == "Double")
  65. {
  66. customDocumentProperties.Add(metadataObj.Property, Double.Parse(metadataObj.Value));
  67. }
  68. else if (metadataObj.ValueType == "Number" || metadataObj.ValueType == "Int32")
  69. {
  70. customDocumentProperties.Add(metadataObj.Property, Int32.Parse(metadataObj.Value));
  71. }
  72. else if (metadataObj.ValueType == "String")
  73. {
  74. customDocumentProperties.Add(metadataObj.Property, metadataObj.Value);
  75. }
  76. }
  77.  
  78. private void AddPdfCustomPropertyValue(Metadata customDocumentProperties, AsposeMetadataObject metadataObj)
  79. {
  80. if (!metadataObj.Property.Contains(':'))
  81. {
  82. metadataObj.Property=metadataObj.Property.Insert(0, "pdfx:");
  83. }
  84.  
  85. if (metadataObj.ValueType == "Boolean")
  86. {
  87. customDocumentProperties.Add(new KeyValuePair<string, XmpValue>(metadataObj.Property,
  88. metadataObj.Value));
  89. }
  90. else if (metadataObj.ValueType == "DateTime")
  91. {
  92. customDocumentProperties.Add(new KeyValuePair<string, XmpValue>(metadataObj.Property, DateTime.Parse(metadataObj.Value)));
  93. }
  94. else if (metadataObj.ValueType == "Double")
  95. {
  96. customDocumentProperties.Add(new KeyValuePair<string, XmpValue>(metadataObj.Property, Double.Parse(metadataObj.Value)));
  97. }
  98. else if (metadataObj.ValueType == "Number" || metadataObj.ValueType == "Int32")
  99. {
  100. customDocumentProperties.Add(new KeyValuePair<string, XmpValue>(metadataObj.Property,
  101. Int32.Parse(metadataObj.Value)));
  102. }
  103. else if (metadataObj.ValueType == "String")
  104. {
  105. customDocumentProperties.Add(metadataObj.Property, metadataObj.Value);
  106. }
  107. }
  108.  
  109. private void AddSlidesCustomPropertyValue(IDocumentProperties customDocumentProperties, AsposeMetadataObject metadataObj)
  110. {
  111. if (metadataObj.ValueType == "Boolean")
  112. {
  113. customDocumentProperties.SetCustomPropertyValue(metadataObj.Property, Boolean.Parse(metadataObj.Value));
  114. }
  115. else if (metadataObj.ValueType == "DateTime")
  116. {
  117. customDocumentProperties.SetCustomPropertyValue(metadataObj.Property, DateTime.Parse(metadataObj.Value));
  118. }
  119. else if (metadataObj.ValueType == "Double")
  120. {
  121. customDocumentProperties.SetCustomPropertyValue(metadataObj.Property, Double.Parse(metadataObj.Value));
  122. }
  123. else if (metadataObj.ValueType == "Number" || metadataObj.ValueType == "Int32")
  124. {
  125. customDocumentProperties.SetCustomPropertyValue(metadataObj.Property, Int32.Parse(metadataObj.Value));
  126. }
  127. else if (metadataObj.ValueType == "String")
  128. {
  129. customDocumentProperties.SetCustomPropertyValue(metadataObj.Property, metadataObj.Value);
  130. }
  131. }
  132.  
  133. private void SetBuiltInPropertyValue(PropertyInfo prop, Object props, string value)
  134. {
  135. try
  136. {
  137. Object obj = null;
  138. if (prop.PropertyType.Name == "Boolean")
  139. {
  140. obj = Boolean.Parse(value);
  141. }
  142. else if (prop.PropertyType.Name == "DateTime")
  143. {
  144. obj = DateTime.Parse(value);
  145. }
  146. else if (prop.PropertyType.Name == "Double")
  147. {
  148. obj = Double.Parse(value);
  149. }
  150. else if (prop.PropertyType.Name == "Number" || prop.PropertyType.Name == "Int32")
  151. {
  152. obj = Int32.Parse(value);
  153. }
  154. else if (prop.PropertyType.Name == "String")
  155. {
  156. obj = value;
  157. }
  158. prop.SetValue(props, obj);
  159. }
  160. catch (Exception) { }
  161. }
  162. ///<Summary>
  163. /// GetAsposeMetadata method to get metadata
  164. ///</Summary>
  165. [HttpGet]
  166. [ActionName("GetAsposeMetadata")]
  167. public async Task<AsposeMetadataResponse> GetAsposeMetadata(string product, string fileNamePath)
  168. {
  169. string logMsg = "ControllerName = " + this.GetType().Name + ", " + "MethodName = " + "GetAsposeMetadata" + ", " + "Folder = " + Path.GetFileName(Path.GetDirectoryName(fileNamePath));
  170. ProductFamilyNameKeysEnum productFamily = ProductFamilyNameKeysEnum.unassigned;
  171. string productName = "";
  172.  
  173. var productInLowerCase = product.ToLowerInvariant();
  174.  
  175. if (productInLowerCase == "words")
  176. {
  177. productName = AsposeWords;
  178. productFamily = ProductFamilyNameKeysEnum.words;
  179.  
  180. }
  181. else if (productInLowerCase == "cells")
  182. {
  183. productName = AsposeCells;
  184. productFamily = ProductFamilyNameKeysEnum.cells;
  185.  
  186. }
  187. else if (productInLowerCase == "slides")
  188. {
  189. productName = AsposeSlides;
  190. productFamily = ProductFamilyNameKeysEnum.slides;
  191. }
  192. else if (productInLowerCase == "pdf")
  193. {
  194. productName = AsposePDF;
  195. productFamily = ProductFamilyNameKeysEnum.pdf;
  196. }
  197. else if (productInLowerCase == "email")
  198. {
  199. productName = AsposeEmail;
  200. productFamily = ProductFamilyNameKeysEnum.email;
  201. }
  202.  
  203. productName += MetadataApp;
  204.  
  205. try
  206. {
  207. AsposeMetadataResponse response = null;
  208.  
  209.  
  210. if (productInLowerCase == "words")
  211. {
  212. response = GetAsposeWordsMetadata(fileNamePath);
  213. }
  214. else if (productInLowerCase == "cells")
  215. {
  216. response = GetAsposeCellsMetadata(fileNamePath);
  217. }
  218. else if (productInLowerCase == "slides")
  219. {
  220. response = GetAsposeSlidesMetadata(fileNamePath);
  221. }
  222. else if (productInLowerCase == "pdf")
  223. {
  224. response = GetAsposePdfMetadata(fileNamePath);
  225. }
  226. else if (productInLowerCase == "email")
  227. {
  228. response = GetAsposeEmailMetadata(fileNamePath);
  229. }
  230.  
  231. NLogger.LogInfo(logMsg, productName, productFamily, Path.GetFileName(fileNamePath));
  232.  
  233. return await Task.FromResult(response);
  234. }
  235. catch (Exception e)
  236. {
  237. NLogger.LogError(e, logMsg, productName, productFamily, Path.GetFileName(fileNamePath));
  238.  
  239. AsposeMetadataResponse response = new AsposeMetadataResponse
  240. {
  241. Status = e.Message,
  242. StatusCode = 500
  243. };
  244.  
  245. return response;
  246. }
  247. }
  248.  
  249. private string GetValueString(object objValue)
  250. {
  251. string retVal = "";
  252. if(objValue != null)
  253. {
  254. retVal = objValue.ToString();
  255. }
  256. return retVal;
  257. }
  258.  
  259. private AsposeMetadataResponse GetAsposeWordsMetadata(string fileNamePath)
  260. {
  261. Words.Document doc = new Aspose.Words.Document(fileNamePath);
  262.  
  263. int i = 0;
  264. List<AsposeMetadataObject> builtInMetadataList = new List<AsposeMetadataObject>();
  265. Words.Properties.BuiltInDocumentProperties props = doc.BuiltInDocumentProperties;
  266. Type t = props.GetType();
  267. foreach(var prop in t.GetProperties())
  268. {
  269. if (prop.CanWrite && IsAllowedType(prop.PropertyType.Name)) {
  270. AsposeMetadataObject metadataObject = new AsposeMetadataObject {
  271. PropertyId = i++,
  272. Property = prop.Name,
  273. Value = GetValueString(prop.GetValue(props)),
  274. ValueType = prop.PropertyType.Name
  275. };
  276. builtInMetadataList.Add(metadataObject);
  277. }
  278. }
  279.  
  280. i = 0;
  281. List<AsposeMetadataObject> customMetadataList = new List<AsposeMetadataObject>();
  282. foreach (Aspose.Words.Properties.DocumentProperty obj in doc.CustomDocumentProperties)
  283. {
  284. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  285. {
  286. PropertyId = i++,
  287. Property = obj.Name,
  288. Value = GetValueString(obj.Value),
  289. ValueType = obj.Type.ToString()
  290. };
  291. customMetadataList.Add(metadataObject);
  292. }
  293.  
  294. AsposeMetadataResponse response = new AsposeMetadataResponse
  295. {
  296. BuiltInMetadataList = builtInMetadataList,
  297. CustomMetadataList = customMetadataList,
  298. Status = "OK",
  299. StatusCode = 200
  300. };
  301. return response;
  302. }
  303.  
  304. private AsposeMetadataResponse GetAsposeCellsMetadata(string fileNamePath)
  305. {
  306. Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(fileNamePath);
  307.  
  308. int i = 0;
  309. List<AsposeMetadataObject> builtInMetadataList = new List<AsposeMetadataObject>();
  310. Cells.Properties.BuiltInDocumentPropertyCollection props = doc.BuiltInDocumentProperties;
  311. Type t = props.GetType();
  312. foreach (var prop in t.GetProperties())
  313. {
  314. if (prop.CanWrite && IsAllowedType(prop.PropertyType.Name))
  315. {
  316. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  317. {
  318. PropertyId = i++,
  319. Property = prop.Name,
  320. Value = GetValueString(prop.GetValue(props)),
  321. ValueType = prop.PropertyType.Name
  322. };
  323. builtInMetadataList.Add(metadataObject);
  324. }
  325. }
  326.  
  327. i = 0;
  328. List<AsposeMetadataObject> customMetadataList = new List<AsposeMetadataObject>();
  329. foreach (Aspose.Cells.Properties.DocumentProperty obj in doc.CustomDocumentProperties)
  330. {
  331. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  332. {
  333. PropertyId = i++,
  334. Property = obj.Name,
  335. Value = GetValueString(obj.Value),
  336. ValueType = obj.Type.ToString()
  337. };
  338. customMetadataList.Add(metadataObject);
  339. }
  340.  
  341. AsposeMetadataResponse response = new AsposeMetadataResponse
  342. {
  343. BuiltInMetadataList = builtInMetadataList,
  344. CustomMetadataList = customMetadataList,
  345. Status = "OK",
  346. StatusCode = 200
  347. };
  348. return response;
  349. }
  350.  
  351. private AsposeMetadataResponse GetAsposePdfMetadata(string fileNamePath)
  352. {
  353. Aspose.Pdf.Document doc = new Aspose.Pdf.Document(fileNamePath);
  354.  
  355. int i = 0;
  356. List<AsposeMetadataObject> builtInMetadataList = new List<AsposeMetadataObject>();
  357. Pdf.DocumentInfo props = doc.Info;
  358. Type t = props.GetType();
  359. foreach (var prop in t.GetProperties())
  360. {
  361.  
  362. if (IsPdfBuiltInProperty(prop.Name) && IsAllowedType((prop.PropertyType.Name)))
  363. {
  364. object obj = null;
  365.  
  366. try
  367. {
  368. obj = prop.GetValue(props);
  369. }
  370. catch (Exception) { }
  371.  
  372. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  373. {
  374. PropertyId = i++,
  375. Property = prop.Name,
  376. Value = GetValueString(obj),
  377. ValueType = prop.PropertyType.Name
  378. };
  379. builtInMetadataList.Add(metadataObject);
  380. }
  381. }
  382.  
  383. i = 0;
  384. List<AsposeMetadataObject> customMetadataList = new List<AsposeMetadataObject>();
  385. foreach (string key in doc.Metadata.Keys)
  386. {
  387. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  388. {
  389. PropertyId = i++,
  390. Property = key,
  391. Value = GetValueString(doc.Metadata[key]),
  392. ValueType = "String"
  393. };
  394. customMetadataList.Add(metadataObject);
  395. }
  396.  
  397. AsposeMetadataResponse response = new AsposeMetadataResponse
  398. {
  399. BuiltInMetadataList = builtInMetadataList,
  400. CustomMetadataList = customMetadataList,
  401. Status = "OK",
  402. StatusCode = 200
  403. };
  404. return response;
  405. }
  406.  
  407. private bool IsPdfBuiltInProperty(string name)
  408. {
  409. var allowedProperties = new HashSet<string> {"Author","CreationDate","Creator","Keywords","ModDate","Subject", "Title","Trapped" };
  410. return allowedProperties.Contains(name);
  411. }
  412.  
  413. private AsposeMetadataResponse GetAsposeSlidesMetadata(string fileNamePath)
  414. {
  415. Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(fileNamePath);
  416.  
  417. int i = 0;
  418. List<AsposeMetadataObject> builtInMetadataList = new List<AsposeMetadataObject>();
  419. var props = doc.DocumentProperties;
  420. Type t = props.GetType();
  421. foreach (var prop in t.GetProperties())
  422. {
  423. if (prop.CanWrite && IsAllowedType(prop.PropertyType.Name))
  424. {
  425. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  426. {
  427. PropertyId = i++,
  428. Property = prop.Name,
  429. Value = GetValueString(prop.GetValue(props)),
  430. ValueType = prop.PropertyType.Name
  431. };
  432. builtInMetadataList.Add(metadataObject);
  433. }
  434. }
  435.  
  436. i = 0;
  437. List<AsposeMetadataObject> customMetadataList = new List<AsposeMetadataObject>();
  438. for (int j=0; j < doc.DocumentProperties.CountOfCustomProperties; j++)
  439. {
  440. string propName = doc.DocumentProperties.GetCustomPropertyName(j);
  441. var prop = doc.DocumentProperties[propName];
  442.  
  443. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  444. {
  445. PropertyId = i++,
  446. Property = propName,
  447. Value = GetValueString(prop),
  448. ValueType = prop.GetType().ToString()
  449. };
  450. customMetadataList.Add(metadataObject);
  451. }
  452.  
  453. AsposeMetadataResponse response = new AsposeMetadataResponse
  454. {
  455. BuiltInMetadataList = builtInMetadataList,
  456. CustomMetadataList = customMetadataList,
  457. Status = "OK",
  458. StatusCode = 200
  459. };
  460. return response;
  461. }
  462.  
  463. private AsposeMetadataResponse GetAsposeEmailMetadata(string fileNamePath)
  464. {
  465. var formatInfo = Email.Tools.FileFormatUtil.DetectFileFormat(fileNamePath);
  466.  
  467. Aspose.Email.Mapi.MapiMessage mail;
  468. if (formatInfo.FileFormatType == Email.FileFormatType.Eml)
  469. mail = Aspose.Email.Mapi.MapiMessage.Load(fileNamePath, new Email.EmlLoadOptions());
  470. else
  471. if (formatInfo.FileFormatType == Email.FileFormatType.Msg)
  472. mail = Aspose.Email.Mapi.MapiMessage.Load(fileNamePath, new Email.MsgLoadOptions());
  473. else
  474. return new AsposeMetadataResponse()
  475. {
  476. StatusCode = 400,
  477. Status = "Invalid file format"
  478. };
  479.  
  480. int i = 0;
  481. List<AsposeMetadataObject> builtInMetadataList = new List<AsposeMetadataObject>();
  482. Type t = mail.GetType();
  483.  
  484. foreach (var prop in t.GetProperties())
  485. {
  486. if (prop.CanWrite && IsAllowedType(prop.PropertyType.Name))
  487. {
  488. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  489. {
  490. PropertyId = i++,
  491. Property = prop.Name,
  492. Value = GetValueString(prop.GetValue(mail)),
  493. ValueType = prop.PropertyType.Name
  494. };
  495. builtInMetadataList.Add(metadataObject);
  496. }
  497. }
  498.  
  499. i = 0;
  500. List<AsposeMetadataObject> customMetadataList = new List<AsposeMetadataObject>();
  501.  
  502. foreach (var pair in mail.GetCustomProperties())
  503. {
  504. var name = pair.Value.Name;
  505. var prop = pair.Value.Property;
  506. var value = prop.GetValue();
  507.  
  508. AsposeMetadataObject metadataObject = new AsposeMetadataObject
  509. {
  510. PropertyId = i++,
  511. Property = name,
  512. Value = GetValueString(prop.Data, value, (MapiPropertyType)prop.DataType),
  513. ValueType = ConvertMailPropertyType((MapiPropertyType)prop.DataType)
  514. };
  515. customMetadataList.Add(metadataObject);
  516. }
  517.  
  518. AsposeMetadataResponse response = new AsposeMetadataResponse
  519. {
  520. BuiltInMetadataList = builtInMetadataList,
  521. CustomMetadataList = customMetadataList,
  522. Status = "OK",
  523. StatusCode = 200
  524. };
  525. return response;
  526. }
  527.  
  528. private string ConvertMailPropertyType(MapiPropertyType type)
  529. {
  530. switch (type)
  531. {
  532. case MapiPropertyType.PT_BOOLEAN:
  533. return "Boolean";
  534. case MapiPropertyType.PT_DOUBLE:
  535. return "Double";
  536. case MapiPropertyType.PT_LONG:
  537. return "Number";
  538. case MapiPropertyType.PT_SYSTIME:
  539. return "DateTime";
  540. case MapiPropertyType.PT_UNICODE:
  541. return "String";
  542. default:
  543. return type.ToString();
  544. }
  545. }
  546. private string GetValueString(byte[] bytesValue, object objValue, MapiPropertyType type)
  547. {
  548. string retVal = "";
  549.  
  550. if (objValue != null)
  551. {
  552. switch (type)
  553. {
  554. case MapiPropertyType.PT_LONG:
  555. retVal = Convert.ToInt64(objValue).ToString();
  556. break;
  557. case MapiPropertyType.PT_BOOLEAN:
  558. retVal = Convert.ToBoolean(objValue).ToString();
  559. break;
  560. case MapiPropertyType.PT_DOUBLE:
  561. retVal = Convert.ToDouble(objValue).ToString("F2");
  562. break;
  563. case MapiPropertyType.PT_SYSTIME:
  564. retVal = Convert.ToDateTime(objValue).ToString();
  565. break;
  566. case MapiPropertyType.PT_UNICODE:
  567. retVal = objValue.ToString();
  568. break;
  569. default:
  570. retVal = Encoding.Unicode.GetString(bytesValue);
  571. break;
  572. }
  573. }
  574.  
  575. return retVal;
  576. }
  577.  
  578. ///////////////////////////////////////////////////////////////////////
  579. ///<Summary>
  580. /// SaveAsposeMetadata method to save metadata properties
  581. ///</Summary>
  582.  
  583. [HttpPost]
  584. [ActionName("SaveAsposeMetadata")]
  585. public Task<AsposeMetadataSaveResponse> SaveAsposeMetadata(AsposeMetadataSaveRequest request)
  586. {
  587. string guid = Guid.NewGuid().ToString();
  588. //string logMsg = "Product: " + request.Product + " FileNamePath: " + request.FileNamePath;
  589. string logMsg = "ControllerName = " + "AsposeMetadataController" + ", " + "MethodName = " + "SaveAsposeMetadata" + ", " + "Folder = " + guid;
  590. ProductFamilyNameKeysEnum productFamily = ProductFamilyNameKeysEnum.unassigned;
  591. string productName = "";
  592.  
  593. var productInLowerCase = request.Product.ToLowerInvariant();
  594.  
  595. if (productInLowerCase == "words")
  596. {
  597. productName = AsposeWords;
  598. productFamily = ProductFamilyNameKeysEnum.words;
  599. }
  600. else if (productInLowerCase == "cells")
  601. {
  602. productName = AsposeCells;
  603. productFamily = ProductFamilyNameKeysEnum.cells;
  604.  
  605. }
  606. else if (productInLowerCase == "slides")
  607. {
  608. productName = AsposeSlides;
  609. productFamily = ProductFamilyNameKeysEnum.slides;
  610. }
  611. else if (productInLowerCase == "pdf")
  612. {
  613. productName = AsposePDF;
  614. productFamily = ProductFamilyNameKeysEnum.pdf;
  615. }
  616. else if (productInLowerCase == "email")
  617. {
  618. productName = AsposeEmail;
  619. productFamily = ProductFamilyNameKeysEnum.email;
  620. }
  621.  
  622. productName += MetadataApp;
  623.  
  624. string outfileName = Path.GetFileName(request.FileNamePath);
  625. try
  626. {
  627. AsposeMetadataSaveResponse response = null;
  628.  
  629.  
  630. string outPath = AppSettings.OutputDirectory + guid;
  631.  
  632. if (!Directory.Exists(outPath))
  633. {
  634. Directory.CreateDirectory(outPath);
  635. outPath += "/" + outfileName;
  636. }
  637.  
  638. System.IO.File.Copy(request.FileNamePath, outPath, true);
  639. string fileNamePath = outPath;
  640.  
  641. if (productInLowerCase == "words")
  642. {
  643. Aspose.App.Models.License.SetAsposeWordsLicense();
  644. response = SaveAsposeWordsMetadata(fileNamePath, request);
  645. }
  646. else if (productInLowerCase == "cells")
  647. {
  648. Aspose.App.Models.License.SetAsposeCellsLicense();
  649. response = SaveAsposeCellsMetadata(fileNamePath, request);
  650. }
  651. else if (productInLowerCase == "slides")
  652. {
  653. Aspose.App.Models.License.SetAsposeSlidesLicense();
  654. response = SaveAsposeSlidesMetadata(fileNamePath, request);
  655. }
  656. else if (productInLowerCase == "pdf")
  657. {
  658. Aspose.App.Models.License.SetAsposePdfLicense();
  659. response = SaveAsposePdfMetadata(fileNamePath, request);
  660. }
  661. else if (productInLowerCase == "email")
  662. {
  663. Aspose.App.Models.License.SetAsposeEmailLicense();
  664. response = SaveAsposeEmailMetadata(fileNamePath, request);
  665. }
  666.  
  667. response.FileName = outfileName;
  668. response.FolderName = guid;
  669.  
  670. NLogger.LogInfo(logMsg,productName , productFamily, outfileName);
  671.  
  672. return Task.FromResult(response);
  673. }
  674. catch (Exception e)
  675. {
  676. NLogger.LogError(e, logMsg, productName, productFamily, outfileName);
  677.  
  678. return Task.FromResult(new AsposeMetadataSaveResponse
  679. {
  680. Status = e.ToString(),
  681. StatusCode = 500,
  682. });
  683. }
  684. }
  685.  
  686. private AsposeMetadataSaveResponse SaveAsposeWordsMetadata(string fileNamePath, AsposeMetadataSaveRequest request)
  687. {
  688. Words.Document doc = new Words.Document(fileNamePath);
  689. Words.Properties.BuiltInDocumentProperties props = doc.BuiltInDocumentProperties;
  690. Type t = props.GetType();
  691.  
  692. foreach (var metadataObj in request.BuiltInMetadataList)
  693. {
  694. var prop = t.GetProperty(metadataObj.Property);
  695. SetBuiltInPropertyValue(prop, props, metadataObj.Value);
  696. }
  697.  
  698. doc.CustomDocumentProperties.Clear();
  699. foreach (var metadataObj in request.CustomMetadataList)
  700. {
  701. AddWordsCustomPropertyValue(doc.CustomDocumentProperties, metadataObj);
  702. }
  703. doc.Save(fileNamePath);
  704.  
  705. return (new AsposeMetadataSaveResponse
  706. {
  707. Status = "OK",
  708. StatusCode = 200,
  709. });
  710. }
  711.  
  712. private AsposeMetadataSaveResponse SaveAsposeCellsMetadata(string fileNamePath, AsposeMetadataSaveRequest request)
  713. {
  714. Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(fileNamePath);
  715. Cells.Properties.BuiltInDocumentPropertyCollection props = doc.BuiltInDocumentProperties;
  716. Type t = props.GetType();
  717.  
  718. foreach (var metadataObj in request.BuiltInMetadataList)
  719. {
  720. var prop = t.GetProperty(metadataObj.Property);
  721. SetBuiltInPropertyValue(prop, props, metadataObj.Value);
  722. }
  723.  
  724. doc.CustomDocumentProperties.Clear();
  725. foreach (var metadataObj in request.CustomMetadataList)
  726. {
  727. AddCellsCustomPropertyValue(doc.CustomDocumentProperties, metadataObj);
  728. }
  729. doc.Save(fileNamePath);
  730.  
  731.  
  732. return (new AsposeMetadataSaveResponse
  733. {
  734. Status = "OK",
  735. StatusCode = 200,
  736. });
  737. }
  738.  
  739. private AsposeMetadataSaveResponse SaveAsposePdfMetadata(string fileNamePath, AsposeMetadataSaveRequest request)
  740. {
  741. Pdf.Document doc = new Aspose.Pdf.Document(fileNamePath);
  742. Pdf.DocumentInfo props = doc.Info;
  743. Type t = props.GetType();
  744.  
  745. foreach (var metadataObj in request.BuiltInMetadataList)
  746. {
  747. try
  748. {
  749. switch (metadataObj.Property)
  750. {
  751. case "Author":
  752. doc.Info.Author = metadataObj.Value;
  753. break;
  754. case "CreationDate":
  755. doc.Info.CreationDate = DateTime.Parse(metadataObj.Value);
  756. break;
  757. case "Keywords":
  758. doc.Info.Keywords = metadataObj.Value;
  759. break;
  760. case "ModDate":
  761. doc.Info.ModDate = DateTime.Parse(metadataObj.Value);
  762. break;
  763. case "Subject":
  764. doc.Info.Subject = metadataObj.Value;
  765. break;
  766. case "Title":
  767. doc.Info.Title = metadataObj.Value;
  768. break;
  769. case "Trapped":
  770. if (!string.IsNullOrWhiteSpace(metadataObj.Value))
  771. {
  772. var str = metadataObj.Value.ToUpper();
  773. if ((str=="TRUE") || (str == "FALSE"))
  774. doc.Info.Trapped = metadataObj.Value;
  775. }
  776. break;
  777. }
  778. }
  779. catch (Exception)
  780. {
  781. throw;
  782. }
  783.  
  784. }
  785.  
  786. //doc.Metadata.Clear();
  787.  
  788. foreach (var metadataObj in request.CustomMetadataList)
  789. {
  790. AddPdfCustomPropertyValue(doc.Metadata, metadataObj);
  791. }
  792. doc.Save(fileNamePath);
  793.  
  794. return (new AsposeMetadataSaveResponse
  795. {
  796. Status = "OK",
  797. StatusCode = 200,
  798. });
  799. }
  800.  
  801. private AsposeMetadataSaveResponse SaveAsposeEmailMetadata(string fileNamePath, AsposeMetadataSaveRequest request)
  802. {
  803. var mail = MapiHelper.GetMapiMessageFromFile(fileNamePath);
  804.  
  805. if (mail == null)
  806. return new AsposeMetadataSaveResponse()
  807. {
  808. StatusCode = 400,
  809. Status = "Invalid file format"
  810. };
  811.  
  812. Type t = mail.GetType();
  813.  
  814. foreach (var metadataObj in request.BuiltInMetadataList)
  815. {
  816. if (metadataObj.Property != "Item")
  817. {
  818. var prop = t.GetProperty(metadataObj.Property);
  819. SetBuiltInPropertyValue(prop, mail, metadataObj.Value);
  820. }
  821. }
  822.  
  823. var customProps = mail.GetCustomProperties();
  824.  
  825. foreach (var item in customProps)
  826. mail.RemoveProperty(item.Key);
  827.  
  828. foreach (var metadataObj in request.CustomMetadataList)
  829. {
  830. var name = metadataObj.Property;
  831.  
  832. switch (metadataObj.ValueType)
  833. {
  834. case "Double":
  835. mail.AddCustomProperty(name, Convert.ToDouble(metadataObj.Value));
  836. break;
  837. case "Boolean":
  838. mail.AddCustomProperty(name, Convert.ToBoolean(metadataObj.Value));
  839. break;
  840. case "Number":
  841. mail.AddCustomProperty(name, Convert.ToInt64(metadataObj.Value));
  842. break;
  843. case "DateTime":
  844. mail.AddCustomProperty(name, Convert.ToDateTime(metadataObj.Value));
  845. break;
  846. case "String":
  847. mail.AddCustomProperty(name, metadataObj.Value);
  848. break;
  849. default:
  850. mail.AddCustomProperty(name, (MapiPropertyType)Enum.Parse(typeof(MapiPropertyType), metadataObj.ValueType), Encoding.Unicode.GetBytes(metadataObj.Value));
  851. break;
  852. }
  853. }
  854.  
  855. mail.Save(fileNamePath);
  856.  
  857. return new AsposeMetadataSaveResponse()
  858. {
  859. Status = "OK",
  860. StatusCode = 200
  861. };
  862. }
  863.  
  864. private AsposeMetadataSaveResponse SaveAsposeSlidesMetadata(string fileNamePath, AsposeMetadataSaveRequest request)
  865. {
  866. var doc = new Aspose.Slides.Presentation(fileNamePath);
  867. var props = doc.DocumentProperties;
  868. Type t = props.GetType();
  869.  
  870. foreach (var metadataObj in request.BuiltInMetadataList)
  871. {
  872. var prop = t.GetProperty(metadataObj.Property);
  873. SetBuiltInPropertyValue(prop, props, metadataObj.Value);
  874. }
  875.  
  876. doc.DocumentProperties.ClearCustomProperties();
  877. foreach (var metadataObj in request.CustomMetadataList)
  878. {
  879. AddSlidesCustomPropertyValue(doc.DocumentProperties, metadataObj);
  880. }
  881.  
  882. string ext = Path.GetExtension(fileNamePath).ToLower();
  883. Aspose.Slides.Export.SaveFormat saveFormat = Aspose.Slides.Export.SaveFormat.Pptx;
  884. switch (ext)
  885. {
  886. case "ppt":
  887. saveFormat = Aspose.Slides.Export.SaveFormat.Ppt;
  888. break;
  889. case "pptx":
  890. saveFormat = Aspose.Slides.Export.SaveFormat.Pptx;
  891. break;
  892. case "odp":
  893. saveFormat = Aspose.Slides.Export.SaveFormat.Odp;
  894. break;
  895. }
  896. doc.Save(fileNamePath, saveFormat);
  897.  
  898. return (new AsposeMetadataSaveResponse
  899. {
  900. Status = "OK",
  901. StatusCode = 200,
  902. });
  903. }
  904.  
  905. }
  906. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement