예제 #1
0
        private const string DefaultExtension = "txt"; // for files without one

        /// <summary>
        /// Asynchronously stores the specified Moodle file with the specified content.
        /// </summary>
        public async Task StoreFileAsync( MoodleFile moodleFile, byte[] content )
        {
            var file = await GetFileAsync( moodleFile, true );
            using ( var stream = await file.OpenStreamForWriteAsync() )
            {
                stream.Write( content, 0, content.Length );
            }
        }
예제 #2
0
        /// <summary>
        /// Asynchronously downloads the specified Moodle file.
        /// </summary>
        public async Task<byte[]> DownloadAsync( MoodleFile file )
        {
            var client = new HttpClient(); // not the PocketCampus HTTP client, the .NET one
            client.DefaultRequestHeaders.Add( SessionHeaderName, _serverSettings.Session );

            var postParams = new Dictionary<string, string>
            {
                { ActionKey, ActionValue },
                { FilePathKey, file.DownloadUrl }
            };
            string downloadUrl = string.Format( DownloadUrlFormat, _serverSettings.Configuration.Protocol, _serverSettings.Configuration.Address, _serverSettings.Configuration.Port );

            var response = await client.PostAsync( downloadUrl, new FormUrlEncodedContent( postParams ) );
            return await response.Content.ReadAsByteArrayAsync();
        }
예제 #3
0
        private static async Task<StorageFile> GetFileAsync( MoodleFile file, bool create )
        {
            // TODO: Find a way to export docs to the Documents library or something
            var folder = ApplicationData.Current.LocalCacheFolder;
            foreach ( string name in file.PathComponents )
            {
                folder = await folder.CreateFolderAsync( FixName( name, Path.GetInvalidPathChars() ), CreationCollisionOption.OpenIfExists );
            }
            string extension = string.IsNullOrWhiteSpace( file.Extension ) ? DefaultExtension : file.Extension;
            string fileName = FixName( file.Name + NameExtensionSeparator + extension, Path.GetInvalidFileNameChars() );

            // GetFileAsync throws an exception if the file doesn't exist, so we have to use GetFilesAsync...
            var storageFile = ( await folder.GetFilesAsync() ).FirstOrDefault( f => f.Name == fileName );
            if ( storageFile == null && create )
            {
                storageFile = await folder.CreateFileAsync( fileName, CreationCollisionOption.ReplaceExisting );
            }
            return storageFile;
        }
예제 #4
0
        /// <summary>
        /// Gets a file. Optionally creates it if it doesn't exist.
        /// </summary>
        private static async Task<StorageFile> GetFileAsync( MoodleFile file, bool create )
        {
            var folder = ApplicationData.Current.LocalFolder;
            foreach ( string name in file.PathComponents )
            {
                folder = await folder.CreateFolderAsync( FixName( name, Path.GetInvalidPathChars() ), CreationCollisionOption.OpenIfExists );
            }
            string extension = string.IsNullOrWhiteSpace( file.Extension ) ? DefaultExtension : file.Extension;
            string fileName = FixName( file.Name, Path.GetInvalidFileNameChars() ) + NameExtensionSeparator + extension;

            // GetFileAsync throws an exception if the file doesn't exist
            // and there's no API to check for a file's existence
            // so we have to use GetFilesAsync...
            var storageFile = ( await folder.GetFilesAsync( CommonFileQuery.DefaultQuery ) )
                                            .FirstOrDefault( f => f.Name == fileName );
            if ( storageFile == null && create )
            {
                storageFile = await folder.CreateFileAsync( fileName );
            }
            return storageFile;
        }
 public Task<byte[]> DownloadAsync( MoodleFile file )
 {
     return Task.FromResult( new byte[0] );
 }
예제 #6
0
 public async Task OpenFileAsync( MoodleFile moodleFile )
 {
     var file = await GetFileAsync( moodleFile, false );
     await Launcher.LaunchFileAsync( file );
 }
예제 #7
0
 public async Task<bool> IsStoredAsync( MoodleFile moodleFile )
 {
     return await GetFileAsync( moodleFile, false ) != null;
 }
예제 #8
0
        private const string DefaultExtension = "txt"; // for files without one


        public async Task StoreFileAsync( MoodleFile moodleFile, byte[] content )
        {
            var file = await GetFileAsync( moodleFile, true );
            await FileIO.WriteBytesAsync( file, content );
        }
예제 #9
0
 public Task StoreFileAsync( MoodleFile file, byte[] content )
 {
     return Task.FromResult( 0 );
 }
예제 #10
0
 public Task OpenFileAsync( MoodleFile file )
 {
     return Task.FromResult( 0 );
 }
예제 #11
0
 public Task<bool> IsStoredAsync( MoodleFile file )
 {
     return Task.FromResult( false );
 }