Skip to content

Commit

Permalink
CSV Gen + Service Using SQLite Only
Browse files Browse the repository at this point in the history
  • Loading branch information
katansapdevelop committed Oct 26, 2023
1 parent 0e8a680 commit be731b8
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 108 deletions.
12 changes: 8 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
Expand All @@ -18,6 +15,13 @@
"skipFiles": [
"<node_internals>/**"
]
},
{
"type": "node-terminal",
"name": "Run CSV Generator",
"request": "launch",
"command": "npm run csv-gen",
"cwd": "${workspaceFolder}"
}
]
}
}
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

A simple demo project to demonstrate how to incorporate media in a CAP App

This was copied from the [CAP Cloud Samples](https://github.com/SAP-samples/cloud-cap-samples/tree/main/media)
This was copied and modified from the [CAP Cloud Samples](https://github.com/SAP-samples/cloud-cap-samples/tree/main/media)


## Running the project
- Open a new terminal and run `cds watch`

- Test Data CSV has already been created

## Running the Test Cases (.http files)
Requires REST Client for VS Code [humao.rest-client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client)

![Media Content from Service](/images/Image3.png)


## Running the CSV generator
- Open a terminal and run `npm run csv-gen`
- This script will read the contents of the `test` directory looking for `.png` image files
- It will then replace the contents of `test\data\sap.capire.media-Media.csv` with details of the files loaded from the `test` directory including the base 64 encoded content

**Note will not run unless you run `npm install` in the `csv-generator` directory**

67 changes: 67 additions & 0 deletions csv-generator/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const path = require('path');
const fs = require('fs');
const { readFile } = require('node:fs/promises');
const mime = require('mime');
const stringify = require('csv-stringify');

const testDataPath = '../test';
const directoryPath = path.join(__dirname, testDataPath);
console.log("Reading files from: " + directoryPath);

const generateCSV = async () => {
fs.readdir(directoryPath, async (err, files) => {
let csvData = [];
let id = 0; // Default to 0

if (err) {
return console.log('Unable to scan directory: ' + err);
}

// Keep only png files
files = files.filter((file) => file.endsWith(".png") );

// Add Header line to CSV
csvData.push(["id","content","mediaType","fileName","applicationName" ]);



console.log("Found the following files: ");
for (let i = 0; i < files.length; i++) {
let fileName = files[i];
id++;
let filePath = path.join(directoryPath, fileName);
let mimeType = mime.getType(filePath);
console.log(filePath + " " + mimeType );

let contents = await readFile(filePath);
let contentBase64 = contents.toString('base64');

// Add header line
csvData.push([id, contentBase64, mimeType, fileName,"demo content" ]);
};


// Convert file to CSV and write test file
stringify.stringify(csvData, (err, output) => {
if (err) {
console.error('Error:', err);
return;
}

// Specify the file path where you want to save the CSV
let filePath = path.join(directoryPath, './data/sap.capire.media-Media.csv');

// Write the CSV to a local file
fs.writeFile(filePath, output, (err) => {
if (err) {
console.error('Error writing to file:', err);
} else {
console.log(`CSV saved to ${filePath}`);
}
});
});

});
}

generateCSV();
33 changes: 33 additions & 0 deletions csv-generator/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions csv-generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "cap-media-csv-generator",
"version": "1.0.0",
"description": "A simple CAP project.",
"repository": "https://github.com/katansapdevelop/cap-media-demo",
"license": "UNLICENSED",
"private": true,
"dependencies": {
"csv-stringify": "^6.4.4",
"mime": "^3.0.0"
},
"scripts": {
"csv-gen": "node generator.js"
}
}

15 changes: 6 additions & 9 deletions db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ namespace sap.capire.media;

entity Media {

key id:Integer;
@Core.MediaType: mediaType
content : LargeBinary ;

@Core.IsMediaType: true
mediaType : String;
fileName : String;
applicationName:String;
}
key id : Integer;
content : LargeBinary @Core.MediaType : mediaType;
mediaType : String @Core.IsMediaType: true;
fileName : String;
applicationName : String;
}
Binary file added images/Image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 12 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "cap-media-demo",
"version": "1.0.0",
"description": "A simple CAP project.",
"repository": "<Add your repository here>",
"description": "A simple CAP project to demonstarte media loaded via CSV",
"repository": "https://github.com/katansapdevelop/cap-media-demo",
"license": "UNLICENSED",
"private": true,
"dependencies": {
"@sap/cds": "^7",
"express": "^4",
"lokijs": "^1.5.12"
"express": "^4"
},
"files": [
"db",
Expand All @@ -19,6 +18,7 @@
"@cap-js/sqlite": "^1"
},
"scripts": {
"start": "cds-serve"
"start": "cds-serve",
"csv-gen": "node ./csv-generator/generator.js"
}
}
68 changes: 0 additions & 68 deletions srv/media-service.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/data/sap.capire.media-Media.csv

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions test/media.http
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Accept: application/json
Content-Type: application/json

{
"id": 1,
"id": 4,
"mediaType": "image/png"
}

### Upload Binary PNG
PUT {{protocol}}://{{host}}:{{port}}/odata/v4/media-server/Media(1)/content
PUT {{protocol}}://{{host}}:{{port}}/odata/v4/media-server/Media(4)/content
Authorization: Basic admin:
Content-Type: image/png

Expand All @@ -30,6 +30,11 @@ Content-Type: image/png
GET {{protocol}}://{{host}}:{{port}}/odata/v4/media-server/Media(1)/content
Authorization: Basic admin:


### Read Binary 2
GET {{protocol}}://{{host}}:{{port}}/odata/v4/media-server/Media(2)/content
Authorization: Basic admin:

### Delete Image
DELETE {{protocol}}://{{host}}:{{port}}/odata/v4/media-server/Media(1)
Authorization: Basic admin:
Binary file added test/sample-bumblebee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/sample-clouds.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit be731b8

Please sign in to comment.