import express from 'express';
import Dropbox from 'dropbox';

const app = express();
const port = process.env.PORT || 8000;

const dbx = new Dropbox({ accessToken: process.env.DROPBOX_API_KEY });

let imageList = [];
let imageIx = 0;

async function loadImages() {
  const response = await dbx.filesListFolder({ path: '/slideshow' });
  const list = [];
  response.entries.forEach((entry) => {
    list.push({ name: entry.name, path: entry.path_lower });
  });
  return list;
}

async function getImageList() {
  if (imageList.length === 0) {
    imageList = await loadImages();
  }
  return imageList;
}

async function getImage() {
  const images = await getImageList();
  let image = images[imageIx];

  // Check if image has expired
  console.log(`Expecting image to expire ${image.expires} and comparing with ${Date.now()}`);
  if (image.expires && image.expires <= Date.now()) {
    image.expires = undefined;
    imageIx += 1;
    if (imageIx >= images.length) {
      imageIx = 0;
    }
    image = images[imageIx];
  }

  // Set expiry on new images
  if (!image.expires) {
    image.expires = Date.now() + (60 * 1000);
  }


  //
  if (!image.url) {
    const response = await dbx.filesGetTemporaryLink({ path: image.path });
    image.url = response.link;
  }
  return image;
}

app.get('/api/image', async (req, res) => {
  try {
    const image = await getImage();
    res.send({ name: image.name, expires: image.expires, url: image.url });
  } catch (err) {
    console.error(err.stack);
    res.status(500).send('Something broke!');
  }
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}!`);
});


@@ -248,22 +248,341 @@
 ion 
-loadImages() %7B
+awaitChanges(cursor) %7B%0A  console.log('Waiting for changes', cursor);%0A  const response = await dbx.filesListFolderLongpoll(%7B cursor %7D);%0A  if (response.changes) %7B%0A    console.log('change detected', response);%0A    imageList = %5B%5D;%0A  %7D else %7B%0A    awaitChanges(cursor);%0A  %7D%0A%7D%0A%0Aasync function loadImages() %7B%0A  console.log('Loading images');
 %0A  c
@@ -646,11 +646,52 @@
 show
-' %7D
+/current' %7D);%0A  awaitChanges(response.cursor
 );%0A 
@@ -817,22 +817,27 @@
   %7D);%0A  
-return
+imageList =
  list;%0A%7D
@@ -905,28 +905,16 @@
 0) %7B%0A   
- imageList =
  await l
@@ -1019,16 +1019,145 @@
 eList();
+%0A%0A  // Safety check%0A  if (imageIx %3E= images.length - 1) %7B%0A    imageIx = 0;%0A  %7D%0A%0A  // Fetch the image - should we have a fallback?
 %0A  let i
@@ -1232,14 +1232,15 @@
 og(%60
-Expect
+Refresh
 ing 
@@ -1249,17 +1249,10 @@
 age 
-to expire
+in
  $%7Bi
@@ -1267,31 +1267,11 @@
 ires
-%7D and comparing with $%7B
+ - 
 Date
@@ -1277,16 +1277,18 @@
 e.now()%7D
+ms
 %60);%0A  if
@@ -1586,17 +1586,16 @@
 );%0A  %7D%0A%0A
-%0A
   //%0A  i
@@ -1718,24 +1718,25 @@
 e.link;%0A  %7D%0A
+%0A
   return ima
@@ -2009,16 +2009,451 @@
  %7D%0A%7D);%0A%0A
+app.get('/api/images', async (req, res) =%3E %7B%0A  try %7B%0A    await getImageList();%0A    res.send(imageList);%0A  %7D catch (err) %7B%0A    console.error(err.stack);%0A    res.status(500).send('Something broke!');%0A  %7D%0A%7D);%0A%0Aapp.get('/api/refresh', async (req, res) =%3E %7B%0A  try %7B%0A    imageList = %5B%5D;%0A    await getImageList();%0A    res.send(imageList);%0A  %7D catch (err) %7B%0A    console.error(err.stack);%0A    res.status(500).send('Something broke!');%0A  %7D%0A%7D);%0A%0A
 app.list

Make server watch dropbox folder