-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSaveManager.cs
130 lines (122 loc) · 4.33 KB
/
FileSaveManager.cs
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
30
31
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
using MauiFileSaver;
using MKFilePicker;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UdpQuickShare.FileActions;
using UdpQuickShare.FileActions.FileSavers;
namespace UdpQuickShare
{
internal class FileSaveManager
{
public class FileResultJson
{
public string FileName { get; set; }
public string FullPath { get; set; }
public string PlatformPath { get; set; }
public static FileResultJson GetFromFilePickFesult(FilePickResult filePickResult)
{
return new FileResultJson
{
FileName = filePickResult.FileName,
FullPath = filePickResult.FullPath,
PlatformPath = filePickResult.PlatformPath,
};
}
}
public const string Videos = "UdpShare/Videos";
public const string Images = "UdpShare/Images";
public const string Texts = "UdpShare/Texts";
public const string Audios = "UdpShare/Audios";
public const string Others = "UdpShare/Others";
public static IDataStore DataStore { get; set; }
public static DefaultFolderProvider DefaultFolderProvider= new DefaultFolderProvider();
public static int MapFileType(FileType fileType)
{
var type=fileType switch
{
FileType.Text => SaveFolderType.Text,
FileType.Audio => SaveFolderType.Audio,
FileType.Video => SaveFolderType.Video,
FileType.Image => SaveFolderType.Image,
_=>SaveFolderType.Other,
};
return (int)type;
}
public static async Task ChooseSaveFolder(FileType fileType)
{
try
{
var res = await MKFilePicker.MKFilePicker.PickFolderAsync(null);
if (res != null)
{
DataStore.Save(fileType.ToString(), FileResultJson.GetFromFilePickFesult(res));
}
}
catch(Exception e)
{
App.Log(e);
}
}
static string GetDefaultChildFolder(FileType fileType)
{
return fileType switch
{
FileType.Text => Texts,
FileType.Image => Images,
FileType.Audio => Audios,
FileType.Video => Videos,
_ => Others,
};
}
public static string GetSaveFolder(FileType fileType)
{
var dataStoreRes = DataStore.Get<FileResultJson>(fileType.ToString());
if(dataStoreRes != null)
{
return dataStoreRes.FullPath;
}
else
{
return Path.Combine(DefaultFolderProvider.GetDefaultFolder(4),GetDefaultChildFolder(fileType));
}
}
public static FileCreateInfo CreateFile(string fileName,FileType fileType)
{
var dataStoreRes = DataStore.Get<FileResultJson>(fileType.ToString());
if (dataStoreRes != null)
{
var file = MKFilePicker.MKFilePicker.CreateFile(dataStoreRes.PlatformPath, fileName);
return new FileCreateInfo()
{
Stream = FileManager.OpenReadWriteFile(file.PlatformPath,file.FullPath),
Path = file.FullPath,
PlatformPath = file.PlatformPath,
FileType = fileType
};
}
else
{
try
{
var file = MauiFileSaver.MKFileSaver.Save((int)SaveFolderType.Other, Path.Combine(GetDefaultChildFolder(fileType), fileName));
return new FileCreateInfo
{
FileType = fileType,
Path = file.FullPath,
PlatformPath=file.PlatformPath,
Stream =FileManager.OpenWriteFile(file.PlatformPath,file.FullPath),
};
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
return null;
}
}
}
}