Skip to content

Commit f1e0d7a

Browse files
committed
add upload feature
1 parent d9e4643 commit f1e0d7a

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

DesktopClient/Main.Designer.cs

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DesktopClient/Main.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ private int LoadMedias()
104104
{
105105
_mediaFiles.Clear();
106106
foreach (string file in Directory.GetFiles(_rootDirectory))
107+
//foreach (string file in Directory.GetFiles(_rootDirectory, "*", SearchOption.AllDirectories))
107108
{
108109
// filter extension
109-
string lowerExtension = Path.GetExtension(file);
110+
string lowerExtension = Path.GetExtension(file).ToLowerInvariant();
110111
if (IgnoreExtensions.Contains(lowerExtension))
111112
{
112113
continue;
@@ -209,6 +210,7 @@ private void ChangeControlsState(bool enabled)
209210

210211
private bool IsValid()
211212
{
213+
_rootDirectory = textBoxPath.Text;
212214
if (!Path.IsPathRooted(_rootDirectory))
213215
{
214216
MessageBox.Show("Invalid path!");
@@ -241,6 +243,52 @@ private void textBox1_DragDrop(object sender, DragEventArgs e)
241243
textBoxPath.Text = directory.First();
242244
}
243245
}
246+
247+
private async void buttonUpload_Click(object sender, EventArgs e)
248+
{
249+
250+
if (!IsValid())
251+
{
252+
return;
253+
}
254+
255+
// change control status to indicate a upload process in about to start
256+
var sb = new StringBuilder();
257+
foreach (var movieInfo in _mediaFiles)
258+
{
259+
foreach (var subtitle in movieInfo.Subtitles)
260+
{
261+
var response = await _client.UploadSubtitleAsync(subtitle, movieInfo.FileInfo.FullName);
262+
if (response.StatusCode == System.Net.HttpStatusCode.Created)
263+
{
264+
sb.AppendFormat(Path.GetFileNameWithoutExtension(subtitle));
265+
}
266+
else
267+
{
268+
sb.AppendFormat($"{Path.GetFileNameWithoutExtension(subtitle)} - {response.StatusCode}");
269+
}
270+
sb.AppendLine();
271+
/*
272+
'Uploaded': (HTTP/1.1 201 Created)
273+
If everything was OK, the HTTP status code 201 will be returned.
274+
275+
'Duplicated': (HTTP/1.1 403 Forbidden)
276+
If the subtitle file already exists in our database, the HTTP status code 403 will be returned.
277+
278+
'Invalid': (HTTP/1.1 415 Unsupported Media Type)
279+
If the subtitle file is not supported by our database, the HTTP status code 415 will be returned.
280+
281+
'Malformed': (HTTP/1.1 400 Bad Request)
282+
If the request was malformed, the HTTP status code 400 will be returned.
283+
*/
284+
}
285+
}
286+
287+
if (sb.Length > 0)
288+
{
289+
MessageBox.Show(sb.ToString());
290+
}
291+
}
244292
}
245293

246294
}

DesktopClient/Models/MediaInfo.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
using System.IO;
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.IO;
24

35
namespace DesktopClient.Models
46
{
57
public class MediaInfo
68
{
79
public string Hash { get; private set; }
810
public FileInfo FileInfo { get; private set; }
11+
public IList<string> Subtitles { get; set; }
912

1013
public static MediaInfo FromFile(string file)
1114
{
1215
return new MediaInfo
1316
{
1417
Hash = SubDBSharp.Helpers.Utils.GetMovieHash(file),
1518
FileInfo = new FileInfo(file),
19+
Subtitles = LoadSubtitles(file)
1620
};
1721
}
1822

23+
private static IList<string> LoadSubtitles(string file)
24+
{
25+
string fileName = Path.GetFileNameWithoutExtension(file);
26+
var list = new List<string>();
27+
foreach (var subtitleFile in Directory.GetFiles(Path.GetDirectoryName(file), $"{fileName}*.srt", SearchOption.TopDirectoryOnly))
28+
{
29+
list.Add(subtitleFile);
30+
}
31+
return list;
32+
}
33+
1934
}
2035
}

src/Http/SubDBApi.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using SubDBSharp.Http;
33
using SubDBSharp.Models;
44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
76
using System.Linq;
87
using System.Net;
@@ -61,7 +60,6 @@ public Task<Response> GetAvailableLanguagesAsync()
6160
/// <returns></returns>
6261
public Task<Response> SearchSubtitle(string hash, bool getVersions = false)
6362
{
64-
6563
Uri fullUrl = _baseAddress.ApplySearchSubtitleParameters(hash, getVersions);
6664
Request request = BuildRequest(fullUrl, HttpMethod.Get, null);
6765
// NOTE: THIS CODE IS ONLY FOR TEST. (SUBDB DOESN'T ENCODED QUERY STRING)
@@ -73,7 +71,6 @@ public Task<Response> SearchSubtitle(string hash, bool getVersions = false)
7371
// // TODO: Language.
7472
//};
7573
//Request request = BuildRequest(new Uri("http://sandbox.thesubdb.com"), HttpMethod.Get, UriExtensions.GetFormURlEncodedContent(requestParameters));
76-
7774
return SendDataAsync(request);
7875
}
7976

@@ -107,7 +104,7 @@ private async Task<Response> SendDataAsync(Request request)
107104
using (HttpRequestMessage requestMessage = BuildRequestMessage(request))
108105
using (HttpResponseMessage responseMessage = await _httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false))
109106
{
110-
return await BuildResponse(responseMessage);
107+
return await BuildResponse(responseMessage).ConfigureAwait(false);
111108
}
112109
}
113110

0 commit comments

Comments
 (0)