index.uts 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import { SetStorageSuccess, SetStorageOptions, SetStorage, SetStorageSync, GetStorageSuccess, GetStorageOptions, GetStorage, GetStorageSync, GetStorageInfoSuccess, GetStorageInfoOptions, GetStorageInfo, GetStorageInfoSync, RemoveStorageSuccess, RemoveStorageOptions, RemoveStorage, RemoveStorageSync, ClearStorage, ClearStorageSync, ClearStorageOptions, ClearStorageSuccess} from "../interface.uts"
import { Storage, StorageManager } from "storage"  assert { type: "implementationOnly" };
import { UTSiOS, dc_storage_aes_key, dc_storage_path_component, dc_storage_old_path_component } from "DCloudUTSFoundation";
import { NSDictionary, URL, FileManager, PropertyListSerialization, NSData, NSKeyedUnarchiver } from 'Foundation';
import { uni_getStorageAsync, uni_getStorageSync, uni_setStorageAsync, uni_setStorageSync } from "../uniStorageTool.uts"


class StorageTool {
	protected static storage: Storage | null
	
	private static migrateStorage(storage: Storage) {
		 let dataPath = UTSiOS.getDataPath()
		 const oldPath = new URL(fileURLWithPath = dataPath).appendingPathComponent(dc_storage_old_path_component).absoluteString
		 let content: any | null = null
		 
		 if (FileManager.default.fileExists(atPath = oldPath)) {
		 	let data = FileManager.default.contents(atPath = oldPath)
			if (data != null) {
				content = PropertyListSerialization.propertyListFromData(data!, mutabilityOption = [PropertyListSerialization.MutabilityOptions.mutableContainersAndLeaves], format = null, errorDescription = null)
			}
			try {
				UTSiOS.try(FileManager.default.removeItem(atPath = oldPath))
			} catch (e) {
				// console.log(e)
			}
		 } else{
			const path = new URL(fileURLWithPath = dataPath).appendingPathComponent(dc_storage_path_component).absoluteString
			let inputData = NSData.init(contentsOfFile = path)
			if ( inputData != null ) {
DCloud-yyl's avatar
DCloud-yyl 已提交
30 31
				let input  = inputData! as Data
				let data = UTSiOS.dc_AESDecrypt(data = input, key = dc_storage_aes_key)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
				if (data != null) {
					content = NSKeyedUnarchiver.unarchiveObject(with = data!)
				}
			} 
			try {
				UTSiOS.try(FileManager.default.removeItem(atPath = path))
			} catch (e) {
				// console.log(e)
			}
		 }
		 if (content != null ) {
		 	let contentDic = UTSiOS.convertDictionary(content!)
		 	if (contentDic.isEmpty == false) {
				 let dic = new NSDictionary(dictionary = contentDic)
				let i = 0
				while (i  < dic.allKeys.length){
					let key = dic.allKeys[i]
					if (key != null && typeof key == "string") {
						let value = dic.value(forKey = key as string)
						if (value != null && typeof value == "string") {
							storage.setItem(key as string, value = value as string, callback = null)
						}
					}
					i++
				}
		 	}
		 }
	}
	
	protected static getStorage(): Storage {
		if (this.storage != null) {
			return this.storage!
		} 
		const domain: string = UTSiOS.getAppId()
		let storage: Storage | null = null
	    storage = StorageManager.storage(withDomain = domain)
		if (this.storage == null) {
			storage = StorageManager.activeStorage(withDomain = domain)
			const path = UTSiOS.getDataPath()
			storage!.rootPath = path
			this.migrateStorage(storage!)
		} 
		this.storage = storage
		return storage!  
	}
	
}  



export const setStorage: SetStorage = function (options: SetStorageOptions) {
	setTimeout(() => {
		uni_setStorageAsync(options, (itemKey: string, itemData: string) => {
			StorageTool.getStorage().setItemPersistent(itemKey, value = itemData, callback = null)
		}, (itemKey: string) => {
			StorageTool.getStorage().removeItem(itemKey, callback = null)
		})  
	}, 0)
}

export const setStorageSync: SetStorageSync = function (key: string, data: any) {
	uni_setStorageSync(key, data, (itemKey: string, itemValue: string) => {
		StorageTool.getStorage().setItemPersistent(itemKey, value = itemValue, callback = null)
	}, (itemKey: string) => {
		StorageTool.getStorage().removeItem(itemKey, callback = null)
	})
}

function getItemAsync(itemKey: string): string | null {
	return StorageTool.getStorage().getItem(itemKey, callback = null)
}

function includeKey(key: string): boolean {
	let storage = StorageTool.getStorage()
	let list = storage.getAllKeys()
	if (list != null) {
		let item = list!.find((value): boolean => {
			if (typeof value == "string") {  
				return (value as string) == key;
			}
			return false;
		})
		return (item != null);
	} 
	return false;
}

export const getStorage: GetStorage = function (options: GetStorageOptions) {
	setTimeout(() => {
		uni_getStorageAsync(options, getItemAsync, includeKey)
	}, 0);
}

export const getStorageSync: GetStorageSync = function (key: string): any {
	return uni_getStorageSync(key, getItemAsync)
}

export const getStorageInfo: GetStorageInfo = function (options: GetStorageInfoOptions) {
	setTimeout(() => {
		const storage = StorageTool.getStorage()
		const allKeys = storage.getAllKeys()
		const length = storage.length()
		const limitSize = 1.7976931348623157e+308
		
		const success: GetStorageInfoSuccess = {
			keys: allKeys,
			currentSize: length,
			limitSize: limitSize
		}
		options.success?.(success)
		options.complete?.(success)
	}, 0);
}

export const getStorageInfoSync: GetStorageInfoSync = function () : GetStorageInfoSuccess {
	const storage = StorageTool.getStorage()
	const allKeys = storage.getAllKeys()
	const length = storage.length()
	const limitSize = 1.7976931348623157e+308
	
	const success: GetStorageInfoSuccess = {
		keys: allKeys,
		currentSize: length,
		limitSize: limitSize
	}
	return success
}

export const removeStorage: RemoveStorage = function (options: RemoveStorageOptions) {
	setTimeout(() => {
		StorageTool.getStorage().removeItem(options.key, callback = null)
		let success: RemoveStorageSuccess = {	
		}
		options.success?.(success)
		options.complete?.(success)
	}, 0);

}

export const removeStorageSync: RemoveStorageSync = function (key: string) {
	StorageTool.getStorage().removeItem(key, callback = null)
}

export const clearStorage: ClearStorage = function (option: ClearStorageOptions | null) {
	setTimeout(() => {
		StorageTool.getStorage().clear()
		let success: ClearStorageSuccess = {
		}
		if (option != null) {
			option!.success?.(success)
			option!.complete?.(success)
		}
	}, 0);
	
}

export const clearStorageSync: ClearStorageSync = function () {
	StorageTool.getStorage().clear()
}