Pre-beta: picture storage should be more responsive if on disk

This commit is contained in:
Robert McRackan 2019-11-13 11:11:00 -05:00
parent e69df2abbc
commit 47360c036d
2 changed files with 46 additions and 51 deletions

View File

@ -42,7 +42,13 @@ namespace FileManager
public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def) public static (bool isDefault, byte[] bytes) GetPicture(PictureDefinition def)
{ {
if (!cache.ContainsKey(def)) if (!cache.ContainsKey(def))
cache.Add(def, null); {
var path = getPath(def);
cache[def]
= FileUtility.FileExists(path)
? File.ReadAllBytes(path)
: null;
}
return (cache[def] == null, cache[def] ?? getDefaultImage(def.Size)); return (cache[def] == null, cache[def] ?? getDefaultImage(def.Size));
} }
@ -64,33 +70,19 @@ namespace FileManager
return; return;
isProcessing = true; isProcessing = true;
PictureDefinition def; var def = cache
string path;
while (true)
{
def = cache
.Where(kvp => kvp.Value is null) .Where(kvp => kvp.Value is null)
.Select(kvp => kvp.Key) .Select(kvp => kvp.Key)
// 80x80 should be 1st since it's enum value == 0 // 80x80 should be 1st since it's enum value == 0
.OrderBy(d => d.PictureId) .OrderBy(d => d.PictureId)
.FirstOrDefault(); .FirstOrDefault();
// no more null entries. all requsted images are cached // no more null entries. all requsted images are cached
if (string.IsNullOrWhiteSpace(def.PictureId)) if (string.IsNullOrWhiteSpace(def.PictureId))
return; return;
path = getPath(def); var bytes = downloadBytes(def);
// we found the next one to download saveFile(def, bytes);
if (!FileUtility.FileExists(path))
break;
// file exists. read into cache. try again
// the point is to throttle web calls. therefore only return if we performed a d/l or there are no null cache entries
cache[def] = File.ReadAllBytes(path);
}
var bytes = download(def);
File.WriteAllBytes(path, bytes);
cache[def] = bytes; cache[def] = bytes;
} }
finally finally
@ -98,12 +90,18 @@ namespace FileManager
isProcessing = false; isProcessing = false;
} }
} }
private static HttpClient imageDownloadClient { get; } = new HttpClient(); private static HttpClient imageDownloadClient { get; } = new HttpClient();
private static byte[] download(PictureDefinition def) private static byte[] downloadBytes(PictureDefinition def)
{ {
var sz = def.Size.ToString().Split('x')[1]; var sz = def.Size.ToString().Split('x')[1];
var bytes = imageDownloadClient.GetByteArrayAsync("ht" + $"tps://images-na.ssl-images-amazon.com/images/I/{def.PictureId}._SL{sz}_.jpg").Result; return imageDownloadClient.GetByteArrayAsync("ht" + $"tps://images-na.ssl-images-amazon.com/images/I/{def.PictureId}._SL{sz}_.jpg").Result;
return bytes; }
private static void saveFile(PictureDefinition def, byte[] bytes)
{
var path = getPath(def);
File.WriteAllBytes(path, bytes);
} }
} }
} }

View File

@ -1,30 +1,27 @@
-- begin BETA --------------------------------------------------------------------------------------------------------------------- -- begin BETA ---------------------------------------------------------------------------------------------------------------------
throttle needed for downloading pdf.s UI
- download now incl pdf
- update wording in menu
picture storage should be more responsive if on disk?
download now incl pdf
pdf menu should be 'PDFs only' pdf menu should be 'PDFs only'
update wording in menu README
update readme - download now incl pdf
- update readme
OLD WEB DOWNLOADER
- download logic in DownloadPdf should look more like DownloadBook
TESTING
possible bug: no mdf,ldf files created in C:\Users\[username]
RELEASE TO BETA
no mdf,ldf files created in C:\Users\[username] - Create release version and installer
https://dotnetcoretutorials.com/2019/06/20/publishing-a-single-exe-file-in-net-core-3-0/
Warn of known performance issues - Warn of known performance issues
- Library import - Library import
- Tag add/edit - Tag add/edit
- Grid is slow to respond loading when books aren't liberated - Grid is slow to respond loading when books aren't liberated
- get decrypt key -- unavoidable - get decrypt key -- unavoidable
Create release version and installer
https://dotnetcoretutorials.com/2019/06/20/publishing-a-single-exe-file-in-net-core-3-0/
-- end BETA --------------------------------------------------------------------------------------------------------------------- -- end BETA ---------------------------------------------------------------------------------------------------------------------
-- begin ENHANCEMENT, IMPORT UI --------------------------------------------------------------------------------------------------------------------- -- begin ENHANCEMENT, IMPORT UI ---------------------------------------------------------------------------------------------------------------------