Skip to content

Commit

Permalink
Smcsw 19/grouped records, image preview and cover images (#43)
Browse files Browse the repository at this point in the history
* Initial backend work and frontend exploration

* More work on group importing

* Working on group records

* More Group work, pushing for sync to other pcs

* Frontend integration of Group into AM

* Potential image working?

Also fixes display bug

* Touch up on image rendering, completion of basic group editing.

- Uploading and Deletion to be completed.

* Possibly a working Group Uploading feature?

- Must beta test.
- Adds in button to Info Page to open configs (QOL Feature)
- Changes policies to be "empty"

* Bug fixes and small changes to get Groups to upload.

* Fixes issue with jenkins crashing on building 2.0.6

* Allows cover images to be uploaded to server.

- See SMCSW-46 .
  • Loading branch information
dmf444 authored Jan 27, 2023
1 parent 4bfcd91 commit 9169313
Show file tree
Hide file tree
Showing 29 changed files with 1,640 additions and 220 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
dist/
node_modules/
out/
.idea/

# Logs
logs
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "archives_manager",
"version": "2.0.6",
"version": "2.0.0",
"description": "The file & metadata management app for archives.",
"main": "./dist/main.bundle.js",
"scripts": {
Expand Down
30 changes: 20 additions & 10 deletions src/main/FileUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,47 @@ const FormData = require('formdata-node');
const log = require('electron-log');

export class FileUploader {
private file: FileModel;
private _settings: UploadSettings;
protected file: FileModel;
protected _settings: UploadSettings;

constructor(file) {
this.file = file;
this._settings = <UploadSettings>getSettingsManager().getSettings("upload");
}

public upload() {
public async upload() {
let data = new FormData();


if(this.file.savedLocation != null){
if (this.file.savedLocation != null) {
data.set('original_file', fs.createReadStream(this.file.savedLocation), this.file.fileName);
}
if(this.file.fileMetadata.extraFile != null && this.file.fileMetadata.extraFile !== "") {
if (this.file.fileMetadata.extraFile != null && this.file.fileMetadata.extraFile !== "") {
data.set('cached_file', fs.createReadStream(this.file.fileMetadata.extraFile));
}
if (this.file.fileMetadata.coverImage != null && this.file.fileMetadata.coverImage !== "") {
data.set('custom_preview', fs.createReadStream(this.file.fileMetadata.coverImage));
}

let saveName = this.file.fileMetadata.localizedName == null ? this.file.fileName : this.file.fileMetadata.localizedName;
data.set('save_name', saveName);
data.set('container', this.file.fileMetadata.container);
data.set('description', this.completeJson(this.file.fileMetadata.description, this.file.fileMetadata.descriptionVersion));
data.set('desc_version', this.file.fileMetadata.descriptionVersion);
if(!this.file.fileMetadata.descriptionVersion.startsWith("1")) {
if (!this.file.fileMetadata.descriptionVersion.startsWith("1")) {
data.set('page_count', this.file.fileMetadata.pageCount);
}
data.set('date', this.file.fileMetadata.date);
data.set('restriction', this.file.fileMetadata.restrictions);
this.file.fileMetadata.tags.forEach(tag => {
data.append('tags[]', tag);
});
if (this.getGroup() != null) {
data.set('group_id', this.getGroup());
}

let urlBase = this._settings.getUrl();
if(urlBase.slice(-1) !== "/") urlBase += "/";
if (urlBase.slice(-1) !== "/") urlBase += "/";
let endPoint = !this.file.fileMetadata.descriptionVersion.startsWith("1") ? "endpoint=document" : "endpoint=image";
/*fetch(urlBase + "api/upload.php?" + endPoint,
{
Expand All @@ -57,12 +63,12 @@ export class FileUploader {
log.info('error parsing', e);
});*/
let headers = data.headers;
if(this._settings.getUsername() !== '') {
if (this._settings.getUsername() !== '') {
headers = new Headers();
headers.append('Content-Type', data.headers["Content-Type"]);
headers.append('Authorization', 'Basic ' + Buffer.from(`${this._settings.getUsername()}:${this._settings.getPassword()}`).toString('base64'));
}
this.connect(urlBase + "api/upload.php?" + endPoint, { method: "post", body: data.stream, headers: headers, mode: "no-cors"});
await this.connect(urlBase + "api/upload.php?" + endPoint, {method: "post", body: data.stream, headers: headers, mode: "no-cors"});
}

async connect(url, data) {
Expand All @@ -82,7 +88,7 @@ export class FileUploader {
}
}

private parseResults(data) {
protected parseResults(data) {
let date = new Date();
let uploadAttempt = { intid: this.file.id, name: this.file.fileName, datetime: date.toLocaleDateString() + " " + date.getHours() + ":" + date.getMinutes() };
let window = getMainWindow();
Expand Down Expand Up @@ -110,4 +116,8 @@ export class FileUploader {
return JSON.stringify(json);
}

protected getGroup(): string {
return null;
}

}
7 changes: 7 additions & 0 deletions src/main/database/IJsonSerializable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export interface IJsonSerializable<V> {

toJson();

// fromJson: (modelJson: {}) => V;
}
61 changes: 59 additions & 2 deletions src/main/database/LocalDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {FileModel} from '@main/file/FileModel';
import {FileState} from '@main/file/FileState';
import {UploadSettings} from "@main/settings/UploadSettings";
import {GoogleOauthSettings} from "@main/settings/GoogleOauthSettings";
import {GroupModel} from '@main/group/models/GroupModel';

const log = require('electron-log');
const low = require('lowdb');
Expand All @@ -13,13 +14,31 @@ const FileSync = require('lowdb/adapters/FileSync');

export class FileDatabase {
private database;
private defaultEmptyType = {
settings: {},
uploadhist: [],
downloadHistory: {},
files: [],
groups: []
}

constructor(filePath: string) {
log.info(filePath);
let adapter = new FileSync(filePath + '/appdb.json');
//log.info("FileDB:" + filePath + '/appdb.json');
this.database = low(adapter);
this.database.defaults({ settings: {}, uploadhist: [], downloadHistory: {}, files: []}).write();
this.database.defaults(this.defaultEmptyType).write();
}

public accessDataPoint(dataPoint: string) {
let dataSpot = this.database.get(dataPoint);
if(dataSpot == null) {
let emptyDataset = {};
emptyDataset[dataPoint] = this.defaultEmptyType[dataPoint];
this.database.push(emptyDataset).write();
dataSpot = this.database.get(dataPoint);
}
return dataSpot;
}

public addFile(file: FileModel) {
Expand Down Expand Up @@ -76,6 +95,7 @@ export class FileDatabase {
return b.value();
}

//TODO: Add groups to this
public isFileUnique(md5Hash: string) {
return this.database.get('files').filter(i => {
return i.md5 == md5Hash;
Expand All @@ -92,6 +112,43 @@ export class FileDatabase {
}).size().value();
}

public addGroup(group: GroupModel) {
let dataSet = this.accessDataPoint('groups');
let model = dataSet.find({id: group.id});
let modelValue = model.value();
if(modelValue == null || modelValue == []) {
dataSet.push(group.toJson()).write();
} else {
model.assign(group.toJson()).write();
}
}

public removeGroup(group: GroupModel) {
this.database.get('groups').remove({id: group.id}).write();
}

public updateGroup(group: GroupModel) {
this.database.get('groups').find({id: group.id}).assign(group.toJson()).write();
}

public getGroupById(id: number): GroupModel {
let model = this.database.get('groups').find({id: id}).value();
return GroupModel.fromJson(model);
}

public getNextFreeGroupId(): number{
let highestModel = this.database.get('groups').orderBy('id', 'desc').take(1);
if(highestModel.size().value() != 0) {
return highestModel.value()[0]['id'] + 1;
} else {
return 0;
}
}

public getAllGroups(): GroupModel[] {
return this.database.get('groups').value();
}

public addNewUpload(data) {
let dataSpot = this.database.get('uploadhist');
if(dataSpot == null){
Expand Down Expand Up @@ -166,4 +223,4 @@ export class FileDatabase {



}
}
19 changes: 15 additions & 4 deletions src/main/description/DescriptionFileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {app} from "electron";
const path = require('path');
const log = require('electron-log');

type descType = {name: string, fields};
type writtenFileContent = {name: string, version: string, fields};
type descType = {name: string, fields: any, fileTypes: string[] | null};
type writtenFileContent = {name: string, version: string, fields: any, fileTypes: string[] | null};
export class DescriptionFileReader {

private versions: Map<string, descType>;
Expand All @@ -14,7 +14,7 @@ export class DescriptionFileReader {
log.info(descFolderPath);
if(jetpack.exists(descFolderPath) !== "dir") {
jetpack.dir(descFolderPath);
jetpack.file(descFolderPath + path.sep + "default_0_0_0.json", {content: {name: "Null", version: "0.0.0", fields: null}, jsonIndent: 4});
jetpack.file(descFolderPath + path.sep + "default_0_0_0.json", {content: {name: "Null", version: "0.0.0", fields: null, fileTypes: null}, jsonIndent: 4});

}
this.readFiles();
Expand All @@ -29,7 +29,8 @@ export class DescriptionFileReader {
let fileNames: string[] = jetpack.list(descFolderPath);
fileNames.forEach(fileName => {
let content: writtenFileContent = jetpack.read(descFolderPath + path.sep + fileName, 'json');
this.versions.set(content.version, {name: content.version + " (" + content.name + ")", fields: content.fields});
let types = content.fileTypes ? content.fileTypes : null;
this.versions.set(content.version, {name: content.version + " (" + content.name + ")", fields: content.fields, fileTypes: types});
});
}

Expand All @@ -40,6 +41,16 @@ export class DescriptionFileReader {
return null;
}

public getDefaultVersion(fileType: string): [string, descType] | null {
let desc: [string, descType] = null;
this.versions.forEach((value, key) => {
if(value.fileTypes && value.fileTypes.includes(fileType)) {
desc = [key, this.versions.get(key)];
}
});
return desc;
}

public getAllVersions() {
let keys = this.versions.keys();
let responseArray = [];
Expand Down
46 changes: 44 additions & 2 deletions src/main/file/FileEditBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import {FileState} from "@main/file/FileState";
import {getFileDatabase, getFileUpdater} from "@main/main";
import {ipcMain} from 'electron';
import {FileUtils} from "@main/downloader/FileUtils";
import {GroupModel} from "@main/group/models/GroupModel";
import {FileUploadData} from "@main/file/FileUploadData";

const log = require('electron-log');

export class FileEditBuilder {

private currentFile: FileModel;
private groupParentId: number;
private fileAdded: boolean = false;
private coverAdded: boolean = false;

constructor(file: FileModel) {
constructor(file: FileModel, groupParent: number = null) {
this.currentFile = file;
this.groupParentId = groupParent;
}


Expand Down Expand Up @@ -72,12 +77,41 @@ export class FileEditBuilder {
return this;
}

public setCoverFilePath(path: string): FileEditBuilder {
this.currentFile.fileMetadata.coverImage = path;
this.coverAdded = true;
return this;
}

public clearCoverFilePath() {
this.currentFile.fileMetadata.coverImage = "";
return this;
}

public commitFile() {
if(this.fileAdded) {
this.currentFile.fileMetadata.extraFile = FileUtils.moveFileByPath(this.currentFile.fileMetadata.extraFile);
this.fileAdded = false;
}
getFileDatabase().updateFile(this.currentFile);
if (this.coverAdded) {
this.currentFile.fileMetadata.coverImage = FileUtils.moveFileByPath(this.currentFile.fileMetadata.coverImage);
this.coverAdded = false;
}
if(this.groupParentId == null) {
getFileDatabase().updateFile(this.currentFile);
} else {
if(this.currentFile.fileMetadata !== FileUploadData.fromJson({})) {
this.setNormalState();
} else {
if(this.currentFile.state == FileState.NORMAL) {
this.currentFile.state = FileState.NEW;
}
}

let group: GroupModel = getFileDatabase().getGroupById(this.groupParentId);
group.replaceFileModel(this.currentFile);
getFileDatabase().updateGroup(group);
}
}
}

Expand Down Expand Up @@ -124,4 +158,12 @@ ipcMain.on('file_edit_extraFile', function (event, arg: string) {

ipcMain.on('file_edit_eFRemove', function (event, arg: string) {
getFileUpdater().clearExtraFilePath();
});

ipcMain.on('file_edit_cover_image', function (event, arg: string) {
if(arg === 'remove') {
getFileUpdater().clearCoverFilePath();
} else {
getFileUpdater().setCoverFilePath(arg);
}
});
4 changes: 4 additions & 0 deletions src/main/file/FileModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export class FileModel {
get fileMetadata(): FileUploadData {
return this._fileMetadata;
}
set fileMetadata(meta: FileUploadData) {
this._fileMetadata = meta;
}

get md5(): string {
return this._md5;
}
Expand Down
Loading

0 comments on commit 9169313

Please sign in to comment.