Skip to content

Commit

Permalink
Remove unnecessary file moving
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucki committed Feb 26, 2023
1 parent e9b1257 commit c7c2855
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 64 deletions.
7 changes: 1 addition & 6 deletions src/adapter/baseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ abstract class BaseAdapter {
protected _logger: Logger;
protected _settings: SettingsModule.Settings;
protected _sourceName: string;
protected _wallpaperLocation: string;

constructor(params: {
defaultName: string;
id: string;
name: string | null;
schemaID: string;
schemaPath: string;
wallpaperLocation: string;
}) {
const path = `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/general/${params.id}/`;
this._logger = new Logger('RWG3', `${params.defaultName} adapter`);

this._wallpaperLocation = params.wallpaperLocation;
this._settings = new SettingsModule.Settings(params.schemaID, params.schemaPath);
this._sourceName = params.name ?? params.defaultName;

Expand All @@ -51,7 +48,7 @@ abstract class BaseAdapter {
* @param {HistoryEntry} historyEntry The historyEntry to fetch
*/
async fetchFile(historyEntry: HistoryEntry) {
const file = Gio.file_new_for_path(`${this._wallpaperLocation}/${String(historyEntry.name)}`);
const file = Gio.file_new_for_path(historyEntry.path);
const fstream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);

// craft new message from details
Expand All @@ -67,8 +64,6 @@ abstract class BaseAdapter {
fstream.write(response_data_bytes, null);
fstream.close(null);

historyEntry.path = file.get_path();

return historyEntry;
}

Expand Down
3 changes: 1 addition & 2 deletions src/adapter/genericJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import {BaseAdapter} from './../adapter/baseAdapter.js';
import {HistoryEntry} from './../history.js';

class GenericJsonAdapter extends BaseAdapter {
constructor(id: string, name: string, wallpaperLocation: string) {
constructor(id: string, name: string) {
super({
defaultName: 'Generic JSON Source',
id,
name,
schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_GENERIC_JSON,
schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/genericJSON/${id}/`,
wallpaperLocation,
});
}

Expand Down
10 changes: 3 additions & 7 deletions src/adapter/localFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import {HistoryEntry} from './../history.js';
Gio._promisify(Gio.File.prototype, 'copy_async', 'copy_finish');

class LocalFolderAdapter extends BaseAdapter {
constructor(id: string, name: string, wallpaperLocation: string) {
constructor(id: string, name: string) {
super({
defaultName: 'Local Folder',
id,
name,
schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_LOCAL_FOLDER,
schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/localFolder/${id}/`,
wallpaperLocation,
});
}

Expand All @@ -43,7 +42,7 @@ class LocalFolderAdapter extends BaseAdapter {
continue;

const historyEntry = new HistoryEntry(null, this._sourceName, randomFilePath);
historyEntry.source.sourceUrl = this._wallpaperLocation;
historyEntry.source.sourceUrl = randomFilePath;

if (!this._includesWallpaper(wallpaperResult, historyEntry.source.imageDownloadUrl))
wallpaperResult.push(historyEntry);
Expand All @@ -60,17 +59,14 @@ class LocalFolderAdapter extends BaseAdapter {

async fetchFile(historyEntry: HistoryEntry) {
const sourceFile = Gio.File.new_for_uri(historyEntry.source.imageDownloadUrl);
const name = sourceFile.get_basename();
const targetFile = Gio.File.new_for_path(this._wallpaperLocation + String(name));
const targetFile = Gio.File.new_for_path(historyEntry.path);

// https://gjs.guide/guides/gio/file-operations.html#copying-and-moving-files
// This function was rewritten by Gio._promisify
// @ts-expect-error
if (!await sourceFile.copy_async(targetFile, Gio.FileCopyFlags.NONE, GLib.PRIORITY_DEFAULT, null, null))
throw new Error('Failed copying image.');

historyEntry.path = targetFile.get_path();

return historyEntry;
}

Expand Down
3 changes: 1 addition & 2 deletions src/adapter/reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ interface RedditSubmission {
}

class RedditAdapter extends BaseAdapter {
constructor(id: string, name: string, wallpaperLocation: string) {
constructor(id: string, name: string) {
super({
defaultName: 'Reddit',
id,
name,
schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_REDDIT,
schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/reddit/${id}/`,
wallpaperLocation,
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/adapter/unsplash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ class UnsplashAdapter extends BaseAdapter {
'constraintValue': '',
};

constructor(id: string | null, name: string | null, wallpaperLocation: string) {
constructor(id: string | null, name: string | null) {
super({
defaultName: 'Unsplash',
id: id ?? '-1',
name,
schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_UNSPLASH,
schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/unsplash/${id}/`,
wallpaperLocation,
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/adapter/urlSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {BaseAdapter} from './../adapter/baseAdapter.js';
import {HistoryEntry} from './../history.js';

class UrlSourceAdapter extends BaseAdapter {
constructor(id: string, name: string, wallpaperLocation: string) {
constructor(id: string, name: string) {
super({
defaultName: 'Static URL',
id,
name,
schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_URL_SOURCE,
schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/urlSource/${id}/`,
wallpaperLocation,
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/adapter/wallhaven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ class WallhavenAdapter extends BaseAdapter {
colors: '',
};

constructor(id: string, name: string, wallpaperLocation: string) {
constructor(id: string, name: string) {
super({
id,
schemaID: SettingsModule.RWG_SETTINGS_SCHEMA_SOURCES_WALLHAVEN,
schemaPath: `${SettingsModule.RWG_SETTINGS_SCHEMA_PATH}/sources/wallhaven/${id}/`,
wallpaperLocation,
name,
defaultName: 'Wallhaven',
});
Expand Down
13 changes: 8 additions & 5 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import * as Utils from './utils.js';

import {Settings} from './settings.js';

// Gets filled by the HistoryController which is constructed at extension startup
let _wallpaperLocation: string;

interface SourceInfo {
author: string | null;
authorUrl: string | null;
Expand All @@ -26,7 +29,7 @@ class HistoryEntry {
id: string;
/** Basename of URI */
name: string | null; // This can be null when an entry from an older version is mapped from settings
path: string | null = null;
path: string;
source: SourceInfo;
adapter: AdapterInfo | null = { // This can be null when an entry from an older version is mapped from settings
id: null,
Expand All @@ -46,6 +49,7 @@ class HistoryEntry {
// extract the name from the url
this.name = Utils.fileName(this.source.imageDownloadUrl);
this.id = `${this.timestamp}_${this.name}`;
this.path = `${_wallpaperLocation}/${this.id}`;
}
}

Expand All @@ -54,10 +58,9 @@ class HistoryController {
size = 10;

private _settings = new Settings();
private _wallpaperLocation: string;

constructor(wallpaperLocation: string) {
this._wallpaperLocation = wallpaperLocation;
_wallpaperLocation = wallpaperLocation;

this.load();
}
Expand Down Expand Up @@ -159,7 +162,7 @@ class HistoryController {
if (firstHistoryElement)
this.history = [firstHistoryElement];

const directory = Gio.file_new_for_path(this._wallpaperLocation);
const directory = Gio.file_new_for_path(_wallpaperLocation);
const enumerator = directory.enumerate_children('', Gio.FileQueryInfoFlags.NONE, null);

let fileInfo;
Expand All @@ -173,7 +176,7 @@ class HistoryController {

// ignore hidden files and first element
if (id[0] !== '.' && id !== firstHistoryElement.id) {
const deleteFile = Gio.file_new_for_path(this._wallpaperLocation + id);
const deleteFile = Gio.file_new_for_path(_wallpaperLocation + id);
deleteFile.delete(null);
}
} while (fileInfo);
Expand Down
47 changes: 11 additions & 36 deletions src/wallpaperController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class WallpaperController {

if (sourceIDs.length < 1 || sourceIDs[0] === '-1') {
randomAdapterResult.push({
adapter: new UnsplashAdapter(null, null, this.wallpaperLocation),
adapter: new UnsplashAdapter(null, null),
id: '-1',
type: 0,
imageCount: count,
Expand Down Expand Up @@ -240,31 +240,31 @@ class WallpaperController {
try {
switch (sourceType) {
case 0:
imageSourceAdapter = new UnsplashAdapter(sourceID, sourceName, this.wallpaperLocation);
imageSourceAdapter = new UnsplashAdapter(sourceID, sourceName);
break;
case 1:
imageSourceAdapter = new WallhavenAdapter(sourceID, sourceName, this.wallpaperLocation);
imageSourceAdapter = new WallhavenAdapter(sourceID, sourceName);
break;
case 2:
imageSourceAdapter = new RedditAdapter(sourceID, sourceName, this.wallpaperLocation);
imageSourceAdapter = new RedditAdapter(sourceID, sourceName);
break;
case 3:
imageSourceAdapter = new GenericJsonAdapter(sourceID, sourceName, this.wallpaperLocation);
imageSourceAdapter = new GenericJsonAdapter(sourceID, sourceName);
break;
case 4:
imageSourceAdapter = new LocalFolderAdapter(sourceID, sourceName, this.wallpaperLocation);
imageSourceAdapter = new LocalFolderAdapter(sourceID, sourceName);
break;
case 5:
imageSourceAdapter = new UrlSourceAdapter(sourceID, sourceName, this.wallpaperLocation);
imageSourceAdapter = new UrlSourceAdapter(sourceID, sourceName);
break;
default:
imageSourceAdapter = new UnsplashAdapter(null, null, this.wallpaperLocation);
imageSourceAdapter = new UnsplashAdapter(null, null);
sourceType = 0;
break;
}
} catch (error) {
this._logger.warn('Had errors, fetching with default settings.');
imageSourceAdapter = new UnsplashAdapter(null, null, this.wallpaperLocation);
imageSourceAdapter = new UnsplashAdapter(null, null);
sourceType = 0;
}

Expand Down Expand Up @@ -475,34 +475,9 @@ class WallpaperController {
const newImageEntries = await Promise.all(fetchPromises);
this._logger.info(`Requested ${newImageEntries.length} new images.`);

// Move file to unique naming
const movePromises = newImageEntries.map(entry => {
if (!entry.path)
return Promise.resolve(false);

const file = Gio.File.new_for_path(entry.path);
const targetFolder = file.get_parent();
const targetFile = targetFolder?.get_child(entry.id);

if (!targetFile)
throw new Error('Failed getting targetFile');

entry.path = targetFile.get_path();

// This function is Gio._promisified
return file.move_async(targetFile, Gio.FileCopyFlags.NONE, 0, null, null);
});

// wait for all images to be moved
await Promise.all(movePromises);

const newWallpaperPaths = newImageEntries.map(element => {
if (element.path)
return element.path;

// eslint-disable-next-line
return;
}) as string[]; // cast because we made sure it's defined
return element.path;
});
const usedWallpaperPaths = this._fillDisplaysFromHistory(newWallpaperPaths, monitorCount);

if (changeType === 3) {
Expand Down

0 comments on commit c7c2855

Please sign in to comment.