Skip to content

Commit

Permalink
Merge pull request #6 from hermanho/dev
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
hermanho authored Aug 16, 2024
2 parents 4b6274d + 384590e commit 2be7b9b
Show file tree
Hide file tree
Showing 14 changed files with 652 additions and 464 deletions.
50 changes: 0 additions & 50 deletions .eslintrc.json

This file was deleted.

35 changes: 35 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Lint

on:
# Trigger the workflow on push or pull request,
# but only for the main branch
push:
branches:
- main
# Replace pull_request with pull_request_target if you
# plan to use this action with forks, see the Limitations section
pull_request:
branches:
- main

jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20

# ESLint and Prettier must be in `package.json`
- name: Install Node.js dependencies
run: npm ci

- uses: sibiraj-s/action-eslint@v3
with:
annotations: true
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# MMM-OneDrive Change Log

**`[1.1.0] - 2024/08/15`**
- Fixed: albumn info did not show when startup caching
- Fixed: depedencies packages update
- Added: Allow regular expression ([RE2 engine](https://github.com/google/re2)) in album names
- Changed: Update README.md and INSTALL.md

**`[1.0.0] - 2023/07/17`**
First version
14 changes: 12 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

1. Install Module

Run the following command. For example, the Magic Mirror directory is `~/MagicMirror`.
```sh
cd ~/MagicMirror/modules
git clone https://github.com/hermanho/MMM-OneDrive
git clone https://github.com/hermanho/MMM-OneDrive.git
cd MMM-OneDrive
npm run install-prod
```
Expand All @@ -13,12 +14,21 @@

```sh
cd ~/MagicMirror/modules
git clone https://github.com/hermanho/MMM-OneDrive
git clone https://github.com/hermanho/MMM-OneDrive.git
docker exec -it -w /opt/magic_mirror/modules/MMM-OneDrive magic_mirror npm run install-prod
```

1. Add MMM-OneDrive module config in ~/MagicMirror/config/config.js

## Upgrade

Run the following command. For example, the Magic Mirror directory is `~/MagicMirror`.
```sh
cd ~/MagicMirror/modules/MMM-OneDrive
git pull
npm run install-prod
```

## Authorise OAuth Token

### On-device (Just do it on Raspberry PI with Desktop UI)
Expand Down
40 changes: 27 additions & 13 deletions MMM-OneDrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ Module.register("MMM-OneDrive", {
this.firstScan = true;
if (this.config.updateInterval < 1000 * 10) this.config.updateInterval = 1000 * 10;
this.config.condition = Object.assign({}, this.defaults.condition, this.config.condition);
this.sendSocketNotification("INIT", this.config);

const config = { ...this.config };
for (let i = 0; i < config.albums.length; i++) {
const album = config.albums[i];
if (album instanceof RegExp) {
config.albums[i] = {
source: album.source,
flags: album.flags,
};
}
}

this.sendSocketNotification("INIT", config);
this.dynamicPosition = 0;
},

Expand All @@ -53,12 +65,17 @@ Module.register("MMM-OneDrive", {
if (noti === "INITIALIZED") {
this.albums = payload;
//set up timer once initialized, more robust against faults
this.updateTimer = setInterval(() => {
this.updatePhotos();
}, this.config.updateInterval);
if (!this.updateTimer || this.updateTimer === null) {
Log.info("Start timer for updating photos.");
this.updateTimer = setInterval(() => {
this.updatePhotos();
}, this.config.updateInterval);
}
}
if (noti === "UPDATE_ALBUMS") {
this.albums = payload;
}
if (noti === "MORE_PICS") {
console.log("MORE_PICSMORE_PICSMORE_PICS", payload);
if (payload && Array.isArray(payload) && payload.length > 0) this.needMorePicsFlag = false;
this.scanned = payload;
this.index = 0;
Expand Down Expand Up @@ -91,6 +108,7 @@ Module.register("MMM-OneDrive", {
},

updatePhotos: function (dir = 0) {
Log.debug("Updating photos..");
this.firstScan = false;

if (this.scanned.length === 0) {
Expand Down Expand Up @@ -153,15 +171,12 @@ Module.register("MMM-OneDrive", {
let current = document.getElementById("ONEDRIVE_PHOTO_CURRENT");
current.textContent = "";
//current.classList.remove("animated")
let dom = document.getElementById("ONEDRIVE_PHOTO");
// let dom = document.getElementById("ONEDRIVE_PHOTO");
back.style.backgroundImage = `url(${url})`;
current.style.backgroundImage = `url(${url})`;
current.classList.add("animated");
let info = document.getElementById("ONEDRIVE_PHOTO_INFO");
let album = this.albums.find((a) => {
if (a.id === target._albumId) return true;
return false;
});
const info = document.getElementById("ONEDRIVE_PHOTO_INFO");
const album = this.albums.find((a) => a.id === target._albumId);
if (this.config.autoInfoPosition) {
let op = (album, target) => {
let now = new Date();
Expand All @@ -177,7 +192,7 @@ Module.register("MMM-OneDrive", {
if (typeof this.config.autoInfoPosition === "function") {
op = this.config.autoInfoPosition;
}
let [top, left, bottom, right] = op(album, target);
const [top, left, bottom, right] = op(album, target);
info.style.setProperty("--top", top);
info.style.setProperty("--left", left);
info.style.setProperty("--bottom", bottom);
Expand All @@ -200,7 +215,6 @@ Module.register("MMM-OneDrive", {
infoText.appendChild(albumTitle);
infoText.appendChild(photoTime);
info.appendChild(infoText);

this.sendSocketNotification("IMAGE_LOADED", { id: target.id, index: this.index });
},

Expand Down
23 changes: 11 additions & 12 deletions OneDrivePhotos.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ const chunk = (arr, size) =>

class Auth extends EventEmitter {
#debug = {};
/** @type {import("@azure/msal-node").AccountInfo} */
#msalAccount = null;
/** @type {AuthProvider} */
#authProvider = null;

Expand All @@ -42,12 +40,9 @@ class Auth extends EventEmitter {
}
this.#authProvider = new AuthProvider(msalConfig);
console.log("[ONEDRIVE:CORE] Auth -> AuthProvider created");
// this.#msalAccount = await this.#authProvider.login();
// console.log("[ONEDRIVE:CORE] AuthProvider login done");
}

get AuthProvider() { return this.#authProvider; }
// get AccountInfo() { return this.#msalAccount };
}

class OneDrivePhotos {
Expand Down Expand Up @@ -129,13 +124,13 @@ class OneDrivePhotos {
async getAlbumType() {
await this.onAuthReady();
let url = protectedResources.listAllAlbums.endpoint.replace("$$userId$$", this.#userId);
/** @type {import("@microsoft/microsoft-graph-types").DriveItem[]} */
/** @type {microsoftgraph.DriveItem[]} */
let list = [];
let found = 0;
/**
*
* @param {string} pageUrl
* @returns {import("@microsoft/microsoft-graph-types").DriveItem[]} DriveItem
* @returns {microsoftgraph.DriveItem[]} DriveItem
*/
const getAlbum = async (pageUrl) => {
this.log("Getting Album info chunks.");
Expand Down Expand Up @@ -191,7 +186,7 @@ class OneDrivePhotos {
/** @type {import("@microsoft/microsoft-graph-client").PageCollection} */
let response = await this.request(pageUrl, "get");
if (Array.isArray(response.value)) {
/** @type {import("@microsoft/microsoft-graph-types").DriveItem[]} */
/** @type {microsoftgraph.DriveItem[]} */
const childrenItems = response.value;
for (let item of childrenItems) {
/** @type {OneDriveMediaItem} */
Expand Down Expand Up @@ -247,8 +242,8 @@ class OneDrivePhotos {
return list; // empty
}
} catch (err) {
this.log(".getImageFromAlbum()", err.toString());
this.log(err);
this.logError(".getImageFromAlbum()", err.toString());
this.logError(err);
throw err;
}
};
Expand All @@ -259,7 +254,7 @@ class OneDrivePhotos {
*
* @param {OneDriveMediaItem[]} items
* @param {string} cachePath
* @returns items
* @returns {OneDriveMediaItem[]} items
*/
async updateTheseMediaItems(items, cachePath) {
if (items.length <= 0) {
Expand Down Expand Up @@ -288,11 +283,15 @@ class OneDrivePhotos {
if (r.status < 400) {
grp[r.id].baseUrl = r.body.value['@microsoft.graph.downloadUrl'];
}
else {
console.error(r);
grp[r.id].baseUrl = null;
}
}
}
}

const heicPhotos = items.filter(i => i.mimeType === "image/heic");
const heicPhotos = items.filter(i => i.mimeType === "image/heic" && i.baseUrl);
for (let photo of heicPhotos) {
const buf = await convertHEIC(photo.baseUrl);
const cacheFilename = encodeURI(path.join(cachePath, photo.id + "-convert.jpg"));
Expand Down
22 changes: 14 additions & 8 deletions PhotosConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ const convert = require('heic-convert');
* @param {string} url
*/
const convertHEIC = async (url) => {
const resp = await fetch(url);
const inputBuffer = Buffer.from(await resp.arrayBuffer());
const outputBuffer = await convert({
buffer: inputBuffer, // the HEIC file buffer
format: 'JPEG', // output format
quality: 0.8, // the jpeg compression quality, between 0 and 1
});
return Buffer.from(outputBuffer);
try {
const resp = await fetch(url);
const inputBuffer = Buffer.from(await resp.arrayBuffer());
const outputBuffer = await convert({
buffer: inputBuffer, // the HEIC file buffer
format: 'JPEG', // output format
quality: 0.8, // the jpeg compression quality, between 0 and 1
});
return Buffer.from(outputBuffer);
} catch (err) {
console.error("Error in convertHEIC for url: ", url);
console.error(err?.stack || err);
throw err;;
}
};

module.exports = { convertHEIC };
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Display your photos from album of OneDrive on [MagicMirror²](https://github.com

![screenshot](images/screenshot2.png)

## Installation
## Installation & Upgrade

[INSTALL.md](INSTALL.md)

Expand Down
53 changes: 53 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import jsdoc from "eslint-plugin-jsdoc";
import globals from "globals";
import js from "@eslint/js";

export default [
js.configs.recommended,
jsdoc.configs['flat/recommended'],
jsdoc.configs['flat/recommended-typescript-flavor'],
{
plugins: {
jsdoc,
},

languageOptions: {
globals: {
...globals.browser,
...globals.node,
Log: true,
MM: true,
Module: true,
moment: true,
},

ecmaVersion: 13,
sourceType: "module",

parserOptions: {
ecmaFeatures: {
globalReturn: true,
},
},
},

rules: {
"comma-dangle": ["error", {
arrays: "always-multiline",
objects: "always-multiline",
imports: "always-multiline",
exports: "always-multiline",
functions: "only-multiline",
}],

eqeqeq: "error",
"no-prototype-builtins": "off",
"no-unused-vars": "off",
"no-useless-return": "error",
"no-var": "error",
"jsdoc/require-returns": "off",
"jsdoc/require-param-description": "off",
semi: "error",
},
},
];
Loading

0 comments on commit 2be7b9b

Please sign in to comment.