|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using ITHit.FileSystem.Windows; |
| 8 | + |
| 9 | + |
| 10 | +namespace ITHit.FileSystem.Samples.Common.Windows |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Provides methods for getting and setting custom data associated with a file or folder. |
| 14 | + /// </summary> |
| 15 | + public static class PlaceholderItemExtensions |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Returns true if the remote item is modified. False - otherwise. |
| 19 | + /// </summary> |
| 20 | + /// <remarks> |
| 21 | + /// This method compares client and server eTags and returns true if the |
| 22 | + /// item in the user file system must be updated with the data from the remote storage. |
| 23 | + /// </remarks> |
| 24 | + /// <param name="placeholder">User file system placeholder item.</param> |
| 25 | + /// <param name="remoteStorageItem">Remote storage item metadata.</param> |
| 26 | + /// <returns></returns> |
| 27 | + public static async Task<bool> IsModifiedAsync(this PlaceholderItem placeholder, FileSystemItemMetadataExt remoteStorageItemMetadata) |
| 28 | + { |
| 29 | + placeholder.TryGetETag(out string clientEtag); |
| 30 | + return clientEtag != remoteStorageItemMetadata.ETag; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Saves all data that is displayed in custom columns in file manager |
| 36 | + /// as well as any additional custom data required by the client. |
| 37 | + /// </summary> |
| 38 | + /// <param name="placeholder">User file system placeholder item.</param> |
| 39 | + /// <param name="remoteStorageItem">Remote storage item metadata.</param> |
| 40 | + /// <returns>A task object that can be awaited.</returns> |
| 41 | + public static async Task SavePropertiesAsync(this PlaceholderItem placeholder, FileSystemItemMetadataExt metadata) |
| 42 | + { |
| 43 | + // Save lock. |
| 44 | + if (metadata.Lock != null) |
| 45 | + { |
| 46 | + placeholder.SetLockInfo(metadata.Lock); |
| 47 | + } |
| 48 | + |
| 49 | + // Save eTag. |
| 50 | + if (metadata.ETag != null) |
| 51 | + { |
| 52 | + placeholder.SetETag(metadata.ETag); |
| 53 | + } |
| 54 | + |
| 55 | + //foreach (FileSystemItemPropertyData prop in metadata.CustomProperties) |
| 56 | + //{ |
| 57 | + // string key = ((CustomColumnIds)prop.Id).ToString(); |
| 58 | + // await placeholder.Properties.AddOrUpdateAsync(key, prop); |
| 59 | + //} |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Tries to get eTag. |
| 64 | + /// </summary> |
| 65 | + /// <param name="placeholder">User file system placeholder item.</param> |
| 66 | + /// <param name="eTag">eTag.</param> |
| 67 | + /// <returns>True if method succeeded. False - otherwise.</returns> |
| 68 | + public static bool TryGetETag(this PlaceholderItem placeholder, out string eTag) |
| 69 | + { |
| 70 | + if (placeholder.Properties.TryGetValue("ETag", out IDataItem propETag)) |
| 71 | + { |
| 72 | + return propETag.TryGetValue<string>(out eTag); |
| 73 | + } |
| 74 | + eTag = null; |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Sets eTag. |
| 80 | + /// </summary> |
| 81 | + /// <param name="placeholder">User file system placeholder item.</param> |
| 82 | + /// <param name="eTag">eTag.</param> |
| 83 | + public static void SetETag(this PlaceholderItem placeholder, string eTag) |
| 84 | + { |
| 85 | + placeholder.Properties.AddOrUpdate("ETag", eTag); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Tries to get lock info. |
| 90 | + /// </summary> |
| 91 | + /// <param name="placeholder">User file system placeholder item.</param> |
| 92 | + /// <param name="serverLockInfo">Lock info.</param> |
| 93 | + /// <returns>True if method succeeded and the item is locked. False if the method failed or the item is not locked.</returns> |
| 94 | + public static bool TryGetLockInfo(this PlaceholderItem placeholder, out ServerLockInfo serverLockInfo) |
| 95 | + { |
| 96 | + if (placeholder.Properties.TryGetValue("LockInfo", out IDataItem propLockInfo)) |
| 97 | + { |
| 98 | + if (propLockInfo.TryGetValue<ServerLockInfo>(out ServerLockInfo lockInfo)) |
| 99 | + { |
| 100 | + if (lockInfo.LockExpirationDateUtc > DateTimeOffset.Now) |
| 101 | + { |
| 102 | + serverLockInfo = lockInfo; |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + serverLockInfo = null; |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Sets lock info. |
| 113 | + /// </summary> |
| 114 | + /// <param name="placeholder">User file system placeholder item.</param> |
| 115 | + /// <param name="serverLockInfo">Lock info.</param> |
| 116 | + public static void SetLockInfo(this PlaceholderItem placeholder, ServerLockInfo serverLockInfo) |
| 117 | + { |
| 118 | + placeholder.Properties.AddOrUpdate("LockInfo", serverLockInfo); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Tries to delete lock info. |
| 123 | + /// </summary> |
| 124 | + /// <param name="placeholder">User file system placeholder item.</param> |
| 125 | + /// <returns>True if method succeeded. False - otherwise.</returns> |
| 126 | + public static bool TryDeleteLockInfo(this PlaceholderItem placeholder) |
| 127 | + { |
| 128 | + return placeholder.Properties.Remove("LockInfo"); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments