Esempio n. 1
0
        private async Task RemoveExpiredWithAccessToken(string accessToken)
        {
            IGoogleDriveHelper driveHelper = new GoogleDriveHelper();

            // Get all the top level folders
            var snapshotFolders = await driveHelper.GetChildrenIDs(accessToken);

            // Decode the folder names for the dates
            foreach (var folder in snapshotFolders.Items)
            {
                DateTime nameAsDate;
                if (DateTime.TryParse(folder.Title, out nameAsDate))
                {
                    if (nameAsDate < DateTime.Today.AddDays(-ExpiryAgeDays))
                    {
                        // Delete this folder
                        Services.Log.Info(string.Format("Deleting expired folder '{0}'", folder.Title));
                        await driveHelper.Delete(accessToken, folder.Id);
                    }
                }
            }

        }
        private async Task<string> GetImageFolderId(string accessToken)
        {
            IGoogleDriveHelper driveHelper = new GoogleDriveHelper();
            var cacheKey = string.Format("statusimage|{0}", accessToken);

            var cachedId = HttpContext.Current.Cache[cacheKey] as string;
            if (!string.IsNullOrEmpty(cachedId))
            {
                return cachedId;
            }

            var lookupId = await driveHelper.GetFolderId(accessToken, ImagesFolder);

            HttpContext.Current.Cache[cacheKey] = lookupId;
            return lookupId;
        }
        private async Task<byte[]> GetStatusImageBytesWithAccessToken(string accessToken, string name)
        {
            IGoogleDriveHelper driveHelper = new GoogleDriveHelper();
            // Get the "images" folder, if it exists
            var imagesFolderId = await GetImageFolderId(accessToken);
            if (!string.IsNullOrEmpty(imagesFolderId))
            {
                var query = string.Format("title = '{0}.png' and '{1}' in parents", name, imagesFolderId);

                var searchResult = await driveHelper.Search(accessToken, query);
                if (searchResult.Items.Length > 0)
                {
                    var imageId = searchResult.Items[0].Id;
                    return await driveHelper.GetFileContent(accessToken, imageId);
                }

            }
            return null;
        }
        private async Task<StatusImageInfo[]> GetStatusImagesWithAccessToken(string accessToken)
        {
            IGoogleDriveHelper driveHelper = new GoogleDriveHelper();

            // Get the "images" folder, if it exists
            var imagesFolderId = await GetImageFolderId(accessToken);
            if (!string.IsNullOrEmpty(imagesFolderId))
            {
                var imageFiles = await driveHelper.GetChildrenIDs(accessToken, imagesFolderId);

                return imageFiles.Items.Select(ToImageInfo).ToArray();
            }

            return null;
        }