~phroa/phone-app

155e4006e09474905d0ef53823e897679738931e — Jack Stratton 2 years ago 4700268 main
Upload files
M phms/FileReceiver.cs => phms/FileReceiver.cs +21 -2
@@ 1,17 1,19 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Net;
using Android.OS;
using Android.Support.V7.App;
using Uri = Android.Net.Uri;

namespace phms
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme")]
    [IntentFilter(new[] {Intent.ActionSend, Intent.ActionSendMultiple}, Categories = new []{Intent.CategoryDefault}, DataMimeType = "*/*")]
    public class FileReceiver : AppCompatActivity
    {
        protected override void OnCreate(Bundle? savedInstanceState)


@@ 36,7 38,24 @@ namespace phms

        private void Upload(IEnumerable<Uri> files)
        {
            throw new System.NotImplementedException();
            foreach (var file in files)
            {
                if (file == null)
                {
                    continue;
                }

                try
                {
                    var url = Uploader.Upload(file);
                    Notes.Send(url);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Notes.Send(e.Message);
                }
            }
        }
    }
}
\ No newline at end of file

M phms/MainActivity.cs => phms/MainActivity.cs +16 -1
@@ 17,6 17,7 @@ namespace phms
    public class MainActivity : AppCompatActivity
    {
        private const string DatabaseUriPref = "db_uri";
        private const string UploadKeyPref = "upload_key";

        protected override void OnCreate(Bundle? savedInstanceState)
        {


@@ 35,7 36,7 @@ namespace phms
            {
                using var prefsEditor = prefs.Edit() ??
                                        throw new Exception("Couldn't Edit preferences.");
                var newValue = databaseUri.Text;
                var newValue = databaseUri.Text!;
                prefsEditor.PutString(DatabaseUriPref, newValue);
                prefsEditor.Apply();
                Database.DatabaseUri = databaseUri.Text!;


@@ 58,6 59,20 @@ namespace phms
                    Notes.Send($"Failed! {ex}");
                }
            };
            
            var uploadKey = FindViewById<EditText>(R.Id.uploadKey) ??
                            throw new Exception("Couldn't FindViewById.");
            uploadKey.Text = prefs.GetString(UploadKeyPref, "");
            Uploader.UploadKey = uploadKey.Text!;
            uploadKey.AfterTextChanged += (sender, e) =>
            {
                using var prefsEditor = prefs.Edit() ??
                                        throw new Exception("Couldn't Edit preferences.");
                var newValue = uploadKey.Text!;
                prefsEditor.PutString(UploadKeyPref, newValue);
                prefsEditor.Apply();
                Uploader.UploadKey = newValue;
            };
        }
    }
}
\ No newline at end of file

M phms/Properties/AndroidManifest.xml => phms/Properties/AndroidManifest.xml +1 -1
@@ 1,7 1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:versionCode="1" 
          android:versionName="1.0" 
          android:versionName="1.0.1" 
          package="phms.phms">
  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
  <uses-permission android:name="android.permission.RECEIVE_SMS" />

M phms/Resources/Resource.Designer.cs => phms/Resources/Resource.Designer.cs +8 -5
@@ 3563,19 3563,22 @@ namespace phms
			public const int up = 2131230901;
			
			// aapt resource value: 0x7F0800B6
			public const int useLogo = 2131230902;
			public const int uploadKey = 2131230902;
			
			// aapt resource value: 0x7F0800B7
			public const int view_offset_helper = 2131230903;
			public const int useLogo = 2131230903;
			
			// aapt resource value: 0x7F0800B8
			public const int visible = 2131230904;
			public const int view_offset_helper = 2131230904;
			
			// aapt resource value: 0x7F0800B9
			public const int withText = 2131230905;
			public const int visible = 2131230905;
			
			// aapt resource value: 0x7F0800BA
			public const int wrap_content = 2131230906;
			public const int withText = 2131230906;
			
			// aapt resource value: 0x7F0800BB
			public const int wrap_content = 2131230907;
			
			static Id()
			{

M phms/Resources/layout/activity_main.axml => phms/Resources/layout/activity_main.axml +9 -0
@@ 17,4 17,13 @@
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/connect" android:layout_below="@id/databaseUri"/>

    <EditText
            android:id="@+id/uploadKey"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textNoSuggestions"
            android:hint="Upload Key"
            android:layout_below="@id/connect"/>

</RelativeLayout>
\ No newline at end of file

A phms/Uploader.cs => phms/Uploader.cs +33 -0
@@ 0,0 1,33 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

using System.Net.Http;
using System.Net.Http.Headers;
using Android.App;
using Android.Net;
using Xamarin.Android.Net;

namespace phms
{
    public class Uploader
    {
        public static string UploadKey { private get; set; }

        public static string Upload(Uri data)
        {
            var resolver = Application.Context.ContentResolver;
            using var stream = resolver.OpenInputStream(data);
            
            var client = new HttpClient(new AndroidClientHandler());
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", UploadKey);

            var file = new StreamContent(stream);
            file.Headers.ContentType = new MediaTypeHeaderValue(resolver.GetType(data) ?? "application/octet-stream");

            var content = new MultipartFormDataContent { { file, "file", "file" } };
            var postTask = client.PostAsync("https://ph.ms", content);
            postTask.Wait();
            var contentTask = postTask.Result.Content.ReadAsStringAsync();
            return contentTask.Result;
        }
    }
}
\ No newline at end of file

M phms/phms.csproj => phms/phms.csproj +3 -0
@@ 59,11 59,13 @@
    <ItemGroup>
        <Compile Include="BackgroundService.cs" />
        <Compile Include="Database.cs" />
        <Compile Include="FileReceiver.cs" />
        <Compile Include="MainActivity.cs" />
        <Compile Include="Notes.cs" />
        <Compile Include="Resources\Resource.Designer.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
        <Compile Include="SmsReceiver.cs" />
        <Compile Include="Uploader.cs" />
    </ItemGroup>
    <ItemGroup>
        <None Include="Resources\AboutResources.txt" />


@@ 81,6 83,7 @@
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="Npgsql" Version="6.0.0-rc.1" />
        <PackageReference Include="System.Net.Http" Version="4.3.4" />
        <PackageReference Include="Xamarin.Android.Support.Design" Version="28.0.0.3" />
    </ItemGroup>
    <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />